#include <stdio.h> /* 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); }
This is undefined code as you are modifying *a twice without a sequence point inbetween the modifies. *** This bug has been marked as a duplicate of 15145 ***
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
Evaluation order is undefined if there is no sequence point.
I have found the following: This works: c ^= d ^= c ^= d (where c and d are not pointers) This fails: *a ^= *b ^= *a ^= *b (where a and b are pointers) When compiling using -Os then the failed case now works.
Since using gcc -Os causes the correct execution, then "sequence point" does not have anything to do with it.
(In reply to joe.carnuccio from comment #5) > Since using gcc -Os causes the correct execution, then "sequence point" does > not have anything to do with it. And you are wrong about that. -Os causes what you think is the correct execution but there are multiple interpretations of the code because there are not sequence points there.
Ok, the sequence points are at each of the assignment operators. The crux of this is that doing the xor chain with dereferenced pointers fails (incorrect execution), whereas doing it with variables works... i.e. *a and *b are being treated differently than a and b; a ^= b ^= a ^= b is supposed to do the following: a = a ^ (b = b ^ (a = a ^ b)) from right-to-left each assignment is done in sequence (and has been verified to work correctly); *a ^= *b ^= *a ^= *b should work the same way, but it does not (unless you compile with -Os).
(In reply to joe.carnuccio from comment #7) > *a ^= *b ^= *a ^= *b should work the same way, but it does not (unless you > compile with -Os). https://gcc.gnu.org/wiki/FAQ#undefinedbut
This should work since C++11 because the rules of builtin assignment were modified (CWG 222; see also CWG 637). However, it is still undefined in C11, even if the new "sequenced before" wording has been copied from C++11 (WG21/N1944). Not sure if any diagnostics should be changed.