Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!psuvax1!rutgers!cmcl2!lanl!cochiti.lanl.gov!jlg From: jlg@cochiti.lanl.gov (Jim Giles) Newsgroups: comp.lang.c Subject: Re: Variable structure size -- ANSI ? Message-ID: <21181@lanl.gov> Date: 12 Apr 91 22:09:04 GMT References: <8334@umd5.umd.edu> <3111@kluge.fiu.edu> <4204@rwthinf.UUCP> <6246@male.EBay.Sun.COM> Sender: news@lanl.gov Organization: Los Alamos National Laboratory Lines: 23 > 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