c/3165: ANSI C standard violation
tczarnecki@abc.com.pl
tczarnecki@abc.com.pl
Wed Jun 13 05:36:00 GMT 2001
>Number: 3165
>Category: c
>Synopsis: ANSI C standard violation
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: change-request
>Submitter-Id: net
>Arrival-Date: Wed Jun 13 05:36:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Tomasz Czarnecki
>Release: unknown-1.0
>Organization:
>Environment:
Sun Solaris 2.6 (Sparc)
>Description:
The order of expression evaluation is bad. The example code produces output like that:
1, a=1, b=1, c=1
2, a=1, b=1, c=1
3, a=1, b=1, c=1
11, a=3, b=2, c=1
1, a=1, b=1, c=1
2, a=1, b=1, c=1
3, a=1, b=1, c=1
22, a=2, b=1, c=1
1, a=1, b=1, c=1
2, a=1, b=1, c=1
3, a=1, b=1, c=1
33, a=1, b=1, c=1
But it should look like:
3, a=1, b=1, c=1
2, a=1, b=1, c=1
1, a=1, b=2, c=1
11, a=3, b=2, c=1
2, a=1, b=1, c=1
3, a=1, b=1, c=1
1, a=1, b=1, c=1
22, a=2, b=1, c=1
3, a=1, b=1, c=1
2, a=1, b=1, c=1
1, a=1, b=1, c=1
33, a=1, b=1, c=1
It looks like exressions are evaluated first and then operators are applied. But I don't think it should work that way. Microsoft VC 6.0 compiler has some problems too but only += like operators case an error. In this case output looks like:
1, a=1, b=1, c=1
2, a=1, b=1, c=1
3, a=1, b=1, c=1
11, a=3, b=2, c=1
2, a=1, b=1, c=1
3, a=1, b=1, c=1
1, a=1, b=1, c=1
22, a=2, b=1, c=1
3, a=1, b=1, c=1
2, a=1, b=1, c=1
1, a=1, b=1, c=1
33, a=1, b=1, c=1
>How-To-Repeat:
/*Compile this file with no options*/
#include <stdio.h>
foo(int n, int a, int b, int c)
{
printf("%d, a=%d, b=%d, c=%d\n", n, a, b, c);
}
int main()
{
int a=1, b=1, c=1;
*(foo(1, a, b, c), &a) += *(foo(2, a, b, c), &b) += *(foo(3, a, b, c), &c);
foo(11, a, b, c);
a=1, b=1, c=1;
*(foo(1, a, b, c), &a) = *(foo(2, a, b, c), &b) + *(foo(3, a, b, c), &c);
foo(22, a, b, c);
a=1, b=1, c=1;
*(foo(1, a, b, c), &a) = *(foo(2, a, b, c), &b) = *(foo(3, a, b, c), &c);
foo(33, a, b, c);
return 0;
}
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-bugs
mailing list