On Thu, Jun 10, 2004 at 04:30:27PM +0200, Stephane Raimbault wrote:
> union {
> double d;
> int64_t i64;
> } swap;
> uint32_t tab_uint32[2];
[...]
> /* Fusion */
> swap.i64 = tab_uint32[0];
> swap.i64 <<= 32;
> swap.i64 |= tab_uint32[1];
>
> /* Cast */
> return swap.d;
This trick may work on some compilers and/or platforms, but it's not
correct C. The language does not guarantee that the members of a union
will be allocated in the exact same address, or even that they will
overlap. What if it registerizes swap.d and/or swap.i64? It may even
recognize one of the two as uninitialized, and the other as a dead value.
That will get you very compact code, but not the effect you want.
To see how it may go wrong, imagine your compiler implemented "union"
as "#define union struct". I think this is all described in the C FAQ,
BTW.
Jeroen