Bug 5159

Summary: ptr[i] = ptr[++i]; // works differently with malloc() and -O
Product: gcc Reporter: robc
Component: rtl-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: gcc-bugs, neil
Priority: P3 Keywords: wrong-code
Version: 2.95.2   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: test.c

Description robc 2001-12-18 14:56:00 UTC
ptr[i] = ptr[++i];
when ptr is a pointer and compiled with -O then it behaves differently.

Release:
gcc version 2.95.2 19991024 (release)

Environment:
Linux 2.2.19, Pentium II

How-To-Repeat:
compile with "gcc -O"
Comment 1 robc 2001-12-18 14:56:00 UTC
Fix:
ptr[i] = ptr[i+1];
i++;
Comment 2 Neil Booth 2001-12-18 15:22:36 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug - you should read up on sequence points.  Basically, multiple reads of a variable (i) modified between sequence points is undefined.  Your code falls into this category.  So,
    
    ptr[i] = ptr[++i];
    
    is fundamentally always meaningless.
Comment 3 Andrew Pinski 2005-04-20 02:56:04 UTC
Reopening to ...
Comment 4 Andrew Pinski 2005-04-20 02:56:26 UTC
Mark as a dup of bug 11751.

*** This bug has been marked as a duplicate of 11751 ***