------- Comment #14 from joseph at codesourcery dot com 2008-11-30 15:37 ------- Subject: Re: O2 causes invalid code
On Sun, 30 Nov 2008, rguenth at gcc dot gnu dot org wrote: > Note that the C standard forbids type-punning through a union. > Basically it says that you may only read from a union member if you have > previously written to it. It also says that all other bits apart from > the ones you have written to contain undefined values after the write. > So > > union { int i; float f; } u; > u.i = 1; > x = u.f; > > invokes undefined behavior in C (but not in GNU C because of the language > extension). Note that C99 TC3 adds a footnote: "*) If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation." -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38297