alignment of struxures

Robert Elz kre at munnari.OZ
Sun Jan 6 20:43:50 AEST 1985


In article <6954 at brl-tgr.ARPA> cottrell at nbs-vms.ARPA writes:

| 2) in the meantime, try this:
| 
| 	#define LONG(x) (*((long *)(x)))
| then
| 	struct ugly {
| 		char	a;
| 		char	longint[4];	/* really a long int */
| 		...
| 	} yuk;
| 
| to get the long int do:
| 
| 	barf = LONG(yuk.longint);

Quite apart from possibly causing "segmentation violation - core dumped"
on those hosts that like aligned longs, it's also not guaranteed to work.

Compilers are allowed to put padding "anywhere" inside a struct/union,
all they need to do is leave the order alone.  One of the compilers
I work with would put 3 bytes of padding between "a" and "longint"
in the structure above...

Robert Elz					decvax!mulga!kre



More information about the Comp.lang.c mailing list