分别对#pragma pack(1)和
#pragma pack(8)进行了测试。
测试平台:Ubuntu 15.04,32位
GCC版本:4.9.2
测试代码:
-
#include <stdio.h>
-
-
// #pragma pack(8)
-
#pragma pack(1)
-
typedef struct s1{
-
short a;
-
long b;
-
} S1;
-
-
typedef struct s2{
-
char c;
-
S1 d;
-
long long e;
-
} S2;
-
#pragma pack()
-
-
int main(void)
-
{
-
S1 s1 = {0xEEFF, 0xAABBCCDD};
-
S2 s2 = {0x11, {0x2233, 0x44556677}, 0x8899EEFF};
-
int s1_size = sizeof(s1);
-
int s2_size = sizeof(s2);
-
int i;
-
unsigned char *s1_p = (unsigned char *)&s1;
-
unsigned char *s2_p = (unsigned char *)&s2;
-
-
printf("char:%d, short:%d, int:%d, long:%d, long long:%d, float:%d, double:%d\n",
-
sizeof(char), sizeof(short), sizeof(int), sizeof(long), sizeof(long long), \
-
sizeof(float), sizeof(double));
-
-
printf("s1 addr: %08x, s2 addr: %08x\n", (unsigned int)s1_p, (unsigned int)s2_p);
-
printf("s1 size: %d byte\n", s1_size);
-
for(i = 0; i < s1_size; i++) {
-
printf("%02x ", s1_p[i]);
-
}
-
printf("\n");
-
-
printf("s2 size: %d byte\n", s2_size);
-
for(i = 0; i < s2_size; i++) {
-
printf("%02x ", s2_p[i]);
-
}
-
printf("\n");
-
-
return 0;
-
}
#pragma pack(1)测试结果:
#pragma pack(8)测试结果:
参考:
[1] http://blog.csdn.net/lime1991/article/details/44536343
[2] http://blog.csdn.net/21aspnet/article/details/6730124
阅读(1530) | 评论(0) | 转发(0) |