[Bug c/39121] strange behavior in chained operations
--- Comment #2 from nospam at pamies dot cat 2009-02-06 21:07 --- Is not the same bug as #15145. I agree with you that there is just one sequence point, but the operation is not undefined. void swap(int *a, int *b) { *a ^= *b ^= *a ^= *b; } This code should be compiled to: *a = *a ^ *b; *b = *b ^ *a; *a = *a ^ *b; And not to something like (I think that is what happens): int tmp; tmp = *a ^ *b; *b = *b ^ tmp; //On that point *a should contain 5^8 instead of the original value 5. //This happens because the temp variable generated by the compiler. *a = *a ^ *b; I think that the compiler is not translating properly what was written in the source code. Summarizing, I think that in: y = 1; x = (y += 1); The execution order should be: volatile_register <--- y + 1 y <--- volatile_register x <--- volatile_register instead of: volatile_register <--- y + 1 x <--- volatile_register y <--- volatile_register -- nospam at pamies dot cat changed: What|Removed |Added Status|RESOLVED|UNCONFIRMED Resolution|DUPLICATE | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39121
[Bug c/39121] New: strange behavior of a chain of operations.
#include /* Why the first swap operation works as expected but it does not happen the same with the second one ? I guess that it can be due operations within temp values, but IMHO swap operation should work in both cases. Thanks ! */ void swap(int *a, int *b) { *a ^= *b ^= *a ^= *b; } int main() { int a = 5; int b = 8; printf("%d, %d\n", a, b); a ^= b ^= a ^= b; printf("%d, %d\n", a, b); swap(&a, &b); printf("%d, %d\n", a, b); } -- Summary: strange behavior of a chain of operations. Product: gcc Version: unknown Status: UNCONFIRMED Severity: major Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: nospam at pamies dot cat http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39121