From: utzoo!decvax!cca!obrien@Rand-Unix@sri-unix
Newsgroups: net.unix-wizards
Title: Re: C sizeof
Article-I.D.: sri-unix.3036
Posted: Wed Sep  1 02:37:26 1982
Received: Thu Sep  9 03:57:15 1982

Date: Thursday, 26 Aug 1982 09:37-PDT
Well, it turns out that the size of the structure you mentioned:

struct x {
	char x1;
	int x2;
} x;

really, truly is 8 bytes.  There are three wasted bytes in the middle.
Even if you reverse the order of the structure elements the result is still
eight bytes long.  If you don't read and write eight bytes when trying to
fill this structure you will lose data.

	As you surmised, this is due to alignment.  The C compiler chooses
to waste space in order to save time.  "sizeof" is not lying and you shouldn't
lose faith in it just because only 5 of the bytes contain "real data".  The
object occupies 8 bytes of memory.  Alignment is NOT "transparent" when it
comes time to externalize the representation of the data, for example by
writing it to disk in its internal form.  It is for this reason that
transferring data from a VAX to a PDP-11 gives great pain, sometimes, unless
you convert everything to character streams via carefully written things
like "tar".