On alignment

Jason Merrill jason@redhat.com
Thu Apr 24 07:00:00 GMT 2003


On 23 Apr 2003 13:39:49 -0600, Tom Tromey <tromey@redhat.com> wrote:

>>>>>> "Gabriel" == Gabriel Dos Reis <gdr@integrable-solutions.net> writes:
>
> Gabriel> #define FIELD_ALIGNOF(TYPE) __alignof (((struct { TYPE t; } *)0)->t)
>
> Gabriel>     j.C: In function `int main()':
> Gabriel>     j.C:7: error: types may not be defined in casts

Ah, yes.  I was using a C testcase.

> In that message you said that you got "8" as the answer for a double
> field on x86.  That's not the answer I'm looking for :-(.  On x86, a
> double field has alignment 4, while a non-field double has alignment 8.
> In the libgcj context, we're only ever laying out structures, so we need
> to get "4" as the answer.  The above does do that.

Again, this currently only works in the trunk.

A similar trick using offsetof should work with all versions of gcc; just
add a char field to the beginning of the struct, and measure the offset to
the TYPE field.

Jason



More information about the Gcc mailing list