alignment/padding of structures

Andrea 'Fyre Wyzard' Bocci fwyzard@inwind.it
Thu Jan 24 14:58:00 GMT 2002


At 17.34 24/01/2002 (GMT +0100), Frederic Christen wrote:
>I've a problem in understanding the behaviour of
>alignment and padding between MSVC++ and gcc-2.9.95 (Linux).

I guess you mean gcc 2.95.x ...


>example:
>
>struct foo {
>         double a;
>         double b;
>         float c;
>};
>
>with MSVC++ and #pragma pack(4) set : sizeof(foo) = 20
>with MSVC++ and #pragma pack(8) set : sizeof(foo) = 24
>(the alternative Zp'x' flag causes the same effect)
>
>but with gcc there is no effect of the #pragma:
>with gcc and #pragma pack(4) set : sizeof(foo) = 20
>with gcc and #pragma pack(8) set : sizeof(foo) = 20

#pragma are compiler dependent.
I don't know whether gcc has a way to allign ALL your variables to a 
certain minimum amount.
For single variable and struct members, you can use the "aligned" attrbiute 
to set the minimum alignment, like this:

struct foo {
         double a __attribute__ ((aligned (8)));
         double b __attribute__ ((aligned (8)));
         float c __attribute__ ((aligned (8)));
}

This will make the 3 fields 8-byte allignd.


>Which compiler-flag has to be set to get the same alignment/
>padding as with MSVC++ ?

Don't know if exist something like this. You can check on the manual.

HTH
fwyzard



More information about the Gcc-help mailing list