A peace of my code uses the two next macros (SWAP and InvSwap) to swap bytes, it worked for a long time under gcc-3.3 but not under gcc-4.0.3 under gcc-3.3: InvWord(0xABCD) = 0xCDAB under gcc-4-0.3: InvWord(0xABCD) = 0xCD00 !!!! ############ FILE STARTS HERE ################## #define SWAP(x,y) (x)^=(y)^=(x)^=(y) #define InvWord(x) SWAP(*((char *)(&(x))),*((char *)(&(x))+1)) int main() { unsigned short data_length; data_length = 0xABCD; InvWord(data_length); return 0; } ############ FILE ENDS HERE ################### I've also tried to use __bswap_16 but id did nothing :( thanks in advance
It's not a gcc bug. The code relies on the results of intermediate subexpressions. According to Stroustrup, The C++ Programming Language, section 6.2.2, "The order of evaluation of subexpressions within an expression is undefined." You should use sequence points e.g.: a ^= b, b ^= a, a ^= b; This is a dup of PR11751
*** This bug has been marked as a duplicate of 11751 ***