The following is a simple C
program:
#include <stdio.h>
typedef struct
{
char a;
double b;
} A;
int main(void) {
printf("sizeof(A) is %d bytes\n", sizeof(A));
return 0;
}
When I compiled it into 32-bit
program, the output is:
sizeof(A) is 12 bytes
I know the structure memory modle should be:
____________________________
|a|3 padding| b |
————————————————————————————
But When I compiled it into 64-bit
program, the output is:
sizeof(A) is 16 bytes
So the structure memory modle should be:
____________________________________
|a|7 padding | b |
____________________________________
Personally, I think no matter the program is 32-bit
or 64-bit
, the size of structure should always be 16
bytes (since char
is 1
byte long, and the alignment of double
is 8
bytes). Why the size is 12
bytes in 32-bit
program?