C语言使用__attribute__ ((packed))
测试的struct 占用字节大小感到挺奇怪, 不能理解 (详细见图)
软件: Dev 5.6.3
编译: TDM-GCC 4.8.1 32
源代码:
#include <stdio.h>
typedef struct
{
char A; //1
int B; //4
char C; //1
double D; //8
char E; //1
}__attribute__ ((packed)) StructA;
struct StructB
{
char A;
int B;
char C;
double D;
char E;
};
int main(void)
{
StructA StructAExample;
struct StructB StructBExample;
printf("short 占用的字节 %d \n", sizeof(short));
printf("int 占用的字节 %d \n", sizeof(int));
printf("double 占用的字节 %d \n", sizeof(double));
printf("StructA 占用的字节 %d \n", sizeof(StructAExample));
printf("StructB 占用的字节 %d \n", sizeof(StructBExample));
return 0;
}