G++ structure member alignment

Baruch Lifshitz baruchli@gmail.com
Tue Feb 13 15:01:00 GMT 2007


Hi,

A quite obvious problem of structure member alignment. I am porting to
Linux some Windows legacy code which created with assumption that that
no padding is performed on structures. In other words if we define

struct t_struct
{
        char ch1;
        short s1;
} t1;

and

typedef t_struct my_var;

then

sizeof(my_var) will be "3" on Intel 586 platform.

Compiling similar code with gcc  3.4.3 on Linux generates variable
sized as 4 bytes.

"Man g++" includes lot of alignment options: for labels, for function,
for jumps and so on but none of them looks to take care about
structures ...

Here is a full code for test program:

#include <stdio.h>

struct t_struct
{
        char l;
        short s1;
} t1_struct;

typedef t_struct my_str;

int main()
{
        my_str my;
        printf("Size if %d\n",sizeof(my));
        return 0;
}

Thank you in advance.



More information about the Gcc-help mailing list