Bug 16309

Summary: different behavior in lvalue assignment in -O0 & -O2
Product: gcc Reporter: Giorgio Alfarano <nakkore>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: gcc-bugs
Priority: P2    
Version: 3.3.3   
Target Milestone: ---   
Host: i386 Target: i386
Build: Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.3/spec Known to work:
Known to fail: Last reconfirmed:

Description Giorgio Alfarano 2004-07-01 08:49:21 UTC
Compiling with -O2 and -O0 the following code i obtain different
result with gcc version > 3.2 ( i tryed 3.3.3,3.3.4)

unsigned char dest[10],source[10]={1,2,3,4,5,6,7,8,9,10};

int main(void)
{
  int i = 1;
  int step = 1;

  while (i < 8)
    dest[i] = source[(i++*step) % 7];


  for(i=0; i < 8; i++)
    printf("%02x ",dest[i]);

  printf("\n");


  i=1;

  while (i < 8)
    {
      dest[i] = source[(i*step) % 7];
      i++;
    }

  for(i=0; i < 8; i++)
    printf("%02x ",dest[i]);

  printf("\n");

  return(i+dest[6]);
}

the difference is how dest[i] is addressed via the index i.
Comment 1 Falk Hueffner 2004-07-01 09:08:17 UTC
With -Wall, you get:

test.c:9: warning: operation on `i' may be undefined

and in fact it is; see documentation on -Wsequence-point.
Comment 2 Wolfgang Bangerth 2004-08-13 16:01:37 UTC
Reopening to mark as a duplicate of... 
Comment 3 Wolfgang Bangerth 2004-08-13 16:04:32 UTC
...PR 11751. 

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