On 1/22/06, Alexandre Oliva <[EMAIL PROTECTED]> wrote: > I don't think it is any different. GCC's exception for unions only > applies if the object is accessed using the union type. So they are > indeed equivalent. The scary thing is that I don't think they > actually violate the ISO C strict aliasing rules, but they might still > be mis-optimized by GCC, assuming I understand correctly what GCC > does.
ISO C says that the following violates aliasing rules: int foo(float f) { union { int i; float f; } u; i.f = f; return i.i; } because the memory at u is accessed as two different effective types. Using a union doesn't change this. The correct solution for converting a float to an integer (bit-representation) as in the expample is to use two different memory locations and memcpy (which avoids aliasing issues by means of using the special rules for access through 'char'). Richard.