This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
AW: "sizeof" get unwanted result
- From: "Kaul, Martin" <martin dot kaul at leuze dot de>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Tue, 19 Apr 2005 08:42:20 +0200
- Subject: AW: "sizeof" get unwanted result
> -----Ursprüngliche Nachricht-----
> Von: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]Im
> Auftrag von Xiaoshan Sun
> Gesendet: Dienstag, 19. April 2005 04:48
> An: gcc-help@gcc.gnu.org
> Betreff: "sizeof" get unwanted result
>
>
> hi,
> 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!!
> so where's the extra 2 bytes? how can i get the gcc to calculate the
> desired size ??
> an aligned issue??
On a 32bit machine the compiler align each element on a 32bit even address. So between data2 and data3 there be 2 Byte filldata. On a 64bit machine the compiler align addresses to 64bit so he fill with 6 Bytes.
To avoid use the packed attribute as proposed by Kevin.
regards
Martin