sizeof(struct) and padding

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Sat Oct 20 07:07:13 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?

You don't.

If you want to add up the sizes of the structure elements, do that.
Write each one to disk individually, and take compactness over speed.

To put it differently, if you want to write a data structure to a file,
you should respect its rights as a *structure*, rather than treating it
as a byte array. As you observed, byte arrays are fine for some things,
like allocating memory, but not for others.

---Dan



More information about the Comp.lang.c mailing list