sizeof(struct) and padding

Walter Bright bright at nazgul.UUCP
Wed Oct 31 19:31:56 AEST 1990


In article <1229 at sun13.scri.fsu.edu> mayne at sun10.scri.fsu.edu (William (Bill) Mayne) writes:
<The $64K question is: How do I portably get the actual size of
<a structure without padding when I need that rather than
<what sizeof() tells me? I have some methods that will work,
<but I am not sure how portable they'd be and I hope someone
<has a better way.

What you need is to turn off the alignment for a struct. There is no
portable way to do it. Compilers typically provide a command line switch
or a pragma to turn off alignment.

If you want to guarantee that alignment is off for a particular struct,
try this:

	struct ABC { int a; char c; };
	...
	assert(sizeof(struct ABC) == sizeof(int) + sizeof(char));



More information about the Comp.lang.c mailing list