On Thursday, February 5, 2004, at 05:23 , Leopold Toetsch wrote:

Nested structs are ugly. The alignment of the first item seems to depend
on the biggest item in the struct.

[...]

Yeah, the struct is aligned to align the largest element therein. e.g.:


struct { char; struct { char; char; char; } }
is at +0, +1, +2, +3

But: struct { char; struct { char; char; short; } }
is at +0, +2, +3, +4

And: struct { char; struct { char; short; char; } }
is at +0, +2, +4, +6


So we need some notion of nested structs or a hint for correct
alignment. I've checked in basic alignment handling for "normal"
cases, but not the second one above.

Maybe you ought to capitulate to the hierarchical nature of it all and simply push on another struct layout specifier to handle the nesting.



struct xt {
        char x;
        struct yt {
                char i;
                int  j;   // use different items here
        } _y;
        char z;
} _x;

int main(void) {
        printf("x : %d\n", offsetof(struct xt, x));
        printf("i : %d\n", offsetof(struct xt, _y.i));
        printf("j : %d\n", offsetof(struct xt, _y.j));
        printf("z : %d\n", offsetof(struct xt, z));
        return 0;
}

Mac OS X output is:


        x : 0
        i : 4
        j : 8
        z : 12



Gordon Henriksen
[EMAIL PROTECTED]

Reply via email to