"sizeof" get unwanted result

Kevin P. Fleming kpfleming@starnetworks.us
Tue Apr 19 05:03:00 GMT 2005

Xiaoshan Sun wrote:

>    i use "sizeof" to get the size of a struct ,defined as:
>    typedef struct _A {
>      long data1;
>      unsigned short data2;
>      long  data3;
>    }A;
>  i guess the sizeof(A) should be 10 .however when i compile and print the
> value of "sizeof(A)" ,the reult is 12!!

That's exactly to be expected, on any 32-bit platform. It could be even 
larger on a 64-bit platform.

If you don't use the 'packed' attribute, the layout and alignment of the 
structure is entirely implementation-dependent.

More information about the Gcc-help mailing list