On Thu, 11 Sep 2003 01:05:07 +0900, "Nathaniel Talbott"
<nathaniel / NOSPAMtalbott.ws> wrote:

>Robert Feldt [mailto:feldt / ce.chalmers.se] wrote:
>
>> I agree it seems pretty straightforward and useful. It still 
>> depends on the answer to the 3 question above. But I guess 
>> it's unlikely a C compiler would do it in any other way.
>> 
>> This "cheat" at least works on gcc 3.2 20020908 on cygwin and 
>> 3.2.3 20030422 on Gentoo linux.
>
>Well, I'm very much a C novice, but from my limited understanding of how
>structs work, it seems that in a case like:
>
>  typedef struct {int a, b;} inner;
>  typedef struct {int c, d; s1 e;} outer;
>
>That outer must be stored like:
>
>  int
>  int
>  int
>  int
>
>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.
>
>
>Nathaniel
>
><:((><
>

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.

HTH.