https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115320

            Bug ID: 115320
           Summary: wrong code with -O2/O3 when using union with different
                    data type
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tangyixuan at mail dot dlut.edu.cn
  Target Milestone: ---

Hi, GCC outputs different results with -O1/O3 during the compilation of the
following test case. Since Union allows different data types to be stored in
the same memory locations, the former variables will be impacted by the
recently assigned variables. However, the point "e" seems to have its own value
whatever the value of point "d" could be with the optimization -O3.

$ gcc -fPIC -mcmodel=large -w -fpermissive -O3 s.c & ./a.out
5
2

$ gcc -fPIC -mcmodel=large -w -fpermissive -O0 s.c & ./a.out
6
6

$ cat s.c

int printf(const char *, ...);
union 
{
  int a;
  short b;
} c;
int *d = &c.a;
short *e = &c.b;
int main()
{
  *d = 0;
  *e=(*e)+1;
  *d = 5;
  *e=(*e)+1; 
  printf("%d\n", *d);
  printf("%d\n", *e);
}

Reply via email to