Variable structure size -- ANSI ?

Jim Giles jlg at cochiti.lanl.gov
Sat Apr 13 08:09:04 AEST 1991


> A question really, does the standard *guarantee* the physical order of structure
> elements ? I looked in K&R 2 and couldn't find anything.

Yes.  The standard guarantees that elements of a struct will be in the
order specified in the declaration.  However, the standard also permits
the compiler to insert padding as it sees fit.  So, using structs to
describe a packed table from another machine (or, from another language
on the same machine) is not reliable.

The above is true for the example given.  However, if the struct contains
bit fields, there is a further complication.  The order of allocation
of bit fields within a "storage unit" (the resolution limit of the
machine's addresses) is implementation defined (it is intended to be
_either_ "little-endian" or "big-endian", but can be any order the
implementor decides).  And, the implementor is still permitted any 
padding he likes - including putting each bit field into a separate
storage unit.

Of course, if you don't need portable code, you can experiment to
determine the actual packing structure of your host machine.

J. Giles



More information about the Comp.lang.c mailing list