This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/32992] New: Incorrect code generated for anonymous union


Compiling and running the code below produces the following output:
    $ g++ -Wall -DT=long -g3 union-bug.C && ./a.out
    &array=   0x7fff11782ef0
    &a=       0x7fff11782f20
    B={1,3,5}
    A={-1719443200,4196007,-1719451616}

A and B should contain the same values, but A contains garbage instead because
the two members of the union do not reside at the same address.

Changing T 'int' produces the expected output:
    $ g++ -Wall -DT=int -g3 union-bug.C && ./a.out
    &array=   0x7fff1fbf6370
    &a=       0x7fff1fbf6370
    B={1,3,5}
    A={1,3,5}

'char' and 'short' also work correctly; '__m128i' (SSE register) breaks. 

This bug affects both gcc-4.1.2 and gcc-4.3

// union-bug.C
#include <cstdio>
struct A {
  T _a;
  T _b;
  T _c;
};
struct B {
  T _array[3];
  operator A() {
    union {
      T array[3];
      A a;
    };
    printf("&array=   %p\n&a=       %p\n", &array, &a);
    for(int i=0; i < 3; i++)
      array[i] = _array[i];
    return a;
  }
};
int main() {
  B b = {{1,3,5}};
  A a = b;
  printf("B={%d,%d,%d}\n", (int) b._array[0], (int) b._array[1], (int)
b._array[2]);
  printf("A={%d,%d,%d}\n", (int) a._a, (int) a._b, (int) a._c);
  return 0;
}


-- 
           Summary: Incorrect code generated for anonymous union
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: scovich at gmail dot com
GCC target triplet: x86_64-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32992


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]