[Bug c/50142] New: There is bug when swap elements of an array via chain expression.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50142 Bug #: 50142 Summary: There is bug when swap elements of an array via chain expression. Classification: Unclassified Product: gcc Version: 4.0.3 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: thomas.c.z...@gmail.com [gcc version]: 4.0.3 [system type]: sparc-sun-solaris2.10 [build options]: none [source code]: main() { int a[] = {1,2}; a[0] ^= a[1] ^= a[0] ^= a[1]; printf("a[0]=%d a[1]=%d\n", a[0], a[1]); } [expected result]: a[0]=2 a[1]=1 [actual result]: a[0]=0 a[1]=1 btw: gcc3 is ok.
[Bug c/50142] There is bug when swap elements of an array via chain expression.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50142 --- Comment #2 from ZHAO Xiaogang 2011-08-22 00:36:34 UTC --- (In reply to comment #1) > Evaluation order is undefined since there is no sequence point involved. I don't think so. The associativity of operator ^= is Right to Left. I have build the code with other compiler. gcc 3.x is OK. Sun cc is OK. Microsoft VC is OK. If there isn't any array, it's also OK when use gcc4 build. main() { int a = 1, b = 2; a ^= b ^= a ^= b; printf("a=%d b=%d\n", a, b); } [expected result]: a=2 b=1 [actual result]: a=2 b=1 So, I guess, the defect associated with the gcc4 new feature which optimized array access.
[Bug c/50142] There is bug when swap elements of an array via chain expression.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50142 ZHAO Xiaogang changed: What|Removed |Added Severity|critical|trivial --- Comment #4 from ZHAO Xiaogang 2011-08-22 07:59:45 UTC --- (In reply to comment #3) > You are wrong, please read the standard, in particular ISO C99 5.1.2.3 and > Annex C. I understand. Thanks.