问题描述:
/* 数据定义 */
#pragma pack(1)
typedef struct stParamList {
unsigned short usParam1;
unsigned short usParam2;
unsigned char ucParam3;
unsigned char ucParam4;
unsigned long aulParam5[6];
}stParamList_S;
stParamList_S g_astParamList[2] =
{
{0x1234,0x1234, 0x56, 0x78,{0x12345678, 0x12345678, 0x12345678,0x12345678,0x12345678,0x12345678}},
{0x1234,0x1234, 0x56, 0x78,{0x87654321, 0x87654321, 0x87654321,0x87654321,0x87654321,0x87654321}}
};
#pragma pack()
unsigned long showTestInfo()
{
printf("\r\n g_astParamList [%#08x]", &g_astParamList);
printf("\r\n size [%u]", sizeof(stParamList_S));
printf("\r\n addr [%#08x]", g_astParamList[0].aulParam5);
return 0;
}
1)调用 showTestInfo 结果如下:
g_astParamList [0x80140000]
size [30] ----这个长度30, 说明pack 1应该没有问题
addr [0x80140006] ----g_astParamList[0].aulParam5确实是从头偏移了6字节
2)查看0x80140000 的内存值如下:
80140000 12 34 12 34 56 78
00 00 12 34 56 78 12 34 56 78
.4.4Vx...4Vx.4Vx ----赋值时这里多了两个字节的0
80140010 12 34 56 78 12 34 56 78 12 34 56 78 12 34 56 78
.4Vx.4Vx.4Vx.4Vx
3)按道理0x80140006
后四个字节值应该是0x12345678,但目前却是0x00001234
答复:MIPS下 #pragma pack(1) 无效,应该使用__attribute__ ((packed))。上面的数据定义成下面的样子应该正常:
typedef struct stParamList {
unsigned short usParam1;
unsigned short usParam2;
unsigned char ucParam3;
unsigned char ucParam4;
unsigned long aulParam5[6];
}__attribute__ ((packed)) stParamList_S;
阅读(1676) | 评论(0) | 转发(0) |