From: Nathaniel Talbott Date: 2003-09-11T04:24:05+09:00 Subject: Re: Calling fun taking struct and not pointer to struct? Tim Hunter [mailto:Tim.Hunter@sas.com] wrote: > >I think nesting a struct simply tells C to allocate an additional > >sizeof(inner) in the outer struct, and the compiler then translates an > >outer.inner.whatever reference to access the correct memory in outer. > >But that's mostly a guess. > > Would that this were true. However, the ANSI C standard > doesn't require it. Consider, for example, > > struct INNER { > int a; > int b; > }; > struct OUTER1 { > int c; > int d; > int e; > struct INNER inner; > }; > > struct OUTER2 { > int c; > int d; > int e; > int f; > int g; > }; > > You can't assume that "f" in OUTER2 is the same as "inner.a" > in OUTER1. The compiler is free to align structure members, > including "inner", any way it wants to, and the alignment > requirements for "f" are certainly not the same as for > "inner". If the compiler chooses, for example, to always > align structures at dword boundaries, then there will be a > word-sized gap between 'e' and 'inner.a' in the OUTER1 struct > (assuming 32-bit machines) that may not appear between "e" > and "f" in the OUTER2 struct. Hmmm... so the question is, is there any way to determine how the compiler aligned things? Nathaniel <:((><