Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!lupine!rfg From: rfg@NCD.COM (Ron Guilmette) Newsgroups: comp.std.c++ Subject: Re: Packing, Ordering, and Rearranging Message-ID: <1407@lupine.NCD.COM> Date: 30 Aug 90 21:22:23 GMT References: <195@xstor.UUCP> <1233@lupine.NCD.COM> Organization: Network Computing Devices, Inc., Mt. View, CA Lines: 56 In article roland@ai.mit.edu (Roland McGrath) writes: +In article <1233@lupine.NCD.COM> rfg@NCD.COM (Ron Guilmette) writes: + + You can definitely specify what you want the exact layout of a struct to be + in C and have it be obeyed and have it be portable. People do it all the + time. Now if only this were possible in C++. :-( + +Since this newsgroup is about the standard for C++, I'll assume you are talking +about standard-conforming implementations of C as well. In this context, your +assertion is patently false. Given the definition: + +struct + { + int elt0; + int elt1; + } foo; + +any of the following layouts are entirely permissable within standard C: + +0: elt0 +4: elt1 + +0: elt0 +8: elt1 + +0: elt0 +1000000:elt1 Roland points out (quite correctly) that a standard conforming C compiler is allowed to insert arbitrary amounts of space between. one member and the next. Still, I my comment was *not* about just the subset of the aspects of the behavior of C compilers which are "standard conforming" aspects. Rather, my comment was meant to include general reality, and not just "standard comformant" reality. The reality I'm speaking of is that given: struct s { long elt0; long elt1; } object; it will be true (for all machines and compilers I care about) that: (char *) &object.elt1 == ((char*) &object.elt0 + 4) In fact, much (so called) "portable" code is written so as to make exactly this kind of assumption. I see nothing wrong with that. It gets the job done, even if it isn't "required" by the standard. How many compilers do you know of that put arbitrary glops of empty space in between struct members? -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.