Chinaunix首页 | 论坛 | 博客
  • 博客访问: 571536
  • 博文数量: 169
  • 博客积分: 2656
  • 博客等级: 少校
  • 技术积分: 1685
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-30 13:03
文章分类

全部博文(169)

文章存档

2011年(1)

2010年(135)

2009年(33)

我的朋友

分类: 嵌入式

2010-05-20 23:13:39

问题描述:
/* 数据定义 */
#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;
阅读(1595) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~