Re: I hate

Brian Alliet <[email protected]> Thu, 13 May 2004 11:36:53 -0400
Newsgroups gmane.comp.java.xwt.core
Message-ID <[email protected]>
On May 13, 2004, at 9:25 AM, Andrew Kohlsmith wrote:

> ... Why shouldn't it work?  I don't know Pascal but I was under the 
> impression
> that was the *specific* reason for unions in C.

Say that in comp.lang.c and you'll get yourself killed :) (check the 
archives in google, they're vicious in there).

> union {
> 	short s1;
> 	int i1;
> 	long l1; } mystuff;
>
> mystuff.l1 = 0xffffffff;
> mystuff.i1 = 0xaaaa;
> mystuff.s1 = 0x00;
>
> mystuff.l1 should now be 0xffffaa00, should it not?

Certainly not. For all you know sizeof(short) == sizeof(int) == 
sizeof(long). Even if they aren't you don't know what endianness the 
target platform is. And even if you know all that. The compiler can 
perfectly legally treat the above as just a plain old struct and not 
have the  memory overlapping at all!

-Brian