有关内存对齐的疑问
#include<stdlib.h>
#include<stdio.h>
typedef struct str1
{
char a;
char b;
double c;
}STR1;
int main()
{
//float a;
printf("The size is: %d\n",sizeof(STR1));
return 0;
}
在网上看到“任何2字节大小的数据类型(比如short)的对齐模数是2,而其它所有超过2字节的数据类型(比如long,double)都以4为对齐模数。”
那么char的对齐模式应该是2或1,double为4
假设char为1,内存布局应该是
--------------
|a |b |////////| c | sizeof(STR1) == (1+1+2+4 = 8)
--------------
1bit 1bit 2bit 4bit
即使char为2,内存布局也应该是这样:
--------------
|a |//|b|//| c | sizeof(STR1) == (1+1+1+1+4 = 8)
--------------
1bit 1bit 4bit
但是结果却是12