sizeof( _variable_ )

Doug Gwyn gwyn at brl-smoke.ARPA
Fri Jul 29 12:34:46 AEST 1988


In article <1264 at bc-cis.UUCP> john at bc-cis.UUCP (John L. Wynstra) writes:
>typedef struct {
>	char	x[10];
>	char	y;
>	char	xx[10];
>	char	yy;
>} Stuff;
>Stuff z;
>Later on in the same code I had a reference to sizeof(z) expecting to get 22
>(which is btw what I just now got on the bsd 4.2 vax), but what I got was 24!

That's not a bug.  The sizeof an object must include any alignment padding.
Since you can make arrays of Stuff, the Stuff is padded out to a multiple
of 4 bytes (the machine word alignment requirement).  I happens that this
particular structure didn't really need the padding, but since all structs
are treated the same in this regard by the compiler, you got the padding
anyway.

You shouldn't make assumptions about the padding inside a struct.



More information about the Comp.lang.c mailing list