Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: comp.lang.c Subject: Re: sizeof(struct) and padding Message-ID: <3654:Oct1921:07:1390@kramden.acf.nyu.edu> Date: 19 Oct 90 21:07:13 GMT References: <1229@sun13.scri.fsu.edu> Organization: IR Lines: 16 In article <1229@sun13.scri.fsu.edu> mayne@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