搜索
bottom↓
回复: 21

sizeof求一个结构体的大小为什么会多了两个字节呢????????

[复制链接]

出0入0汤圆

发表于 2010-10-28 10:08:06 | 显示全部楼层 |阅读模式
如题。我定义了个结构体:

typedef        struct
{
        int16u        ChaPa_Z;
        int16u        ChaPa_K;
        int16u        JingPa_Z;
        int16u        JingPa_K;
        int16u        Temp_Z;
        int16u        Temp_K;
        int16u        Kv;
        int16u        Kp;
        int32u        Ba;
        int16u        CRCFM;
}Glo_T;
sizeof(Glo_T);是24,仿真和下载程序后测试都多了两个字节!!!!!!
我用的编译器是keil uv3。

数据类型定义为:
typedef unsigned long     int32u;   // Unsigned 32 bit quantity
typedef unsigned long     * pint32u;
typedef signed   short    int16s;   // Signed   16 bit quantity
typedef signed   short    * pint16s;
typedef unsigned short    int16u;   // Unsigned 16 bit quantity
typedef unsigned short    * pint16u;

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

 楼主| 发表于 2010-10-28 10:09:05 | 显示全部楼层
先顶。等高人解答。

出0入0汤圆

 楼主| 发表于 2010-10-28 10:11:53 | 显示全部楼层
我这样测试
typedef        struct
{
int16u        CRCFM;
}Glo_T;

sizeof(Glo_T);的大小是4!!!!!!!!!!!!!!!!!!!

出0入0汤圆

发表于 2010-10-28 10:13:23 | 显示全部楼层
看来是32位处理的啊,不够就补齐

出0入0汤圆

发表于 2010-10-28 10:14:58 | 显示全部楼层
看来是32位处理的啊,不够就补齐

出0入0汤圆

发表于 2010-10-28 10:18:11 | 显示全部楼层
typedef struct
{
   …………
  int32u Ba;     //32位变量,结构体整体对齐方式是4字节(也就是说结构体整体占用空间将是4的整数倍)
  int16u CRCFM;
}Glo_T;

出0入0汤圆

发表于 2010-10-28 10:19:24 | 显示全部楼层
typedef struct
{
   …………
  int32u Ba;     //32位变量,结构体整体对齐方式是4字节(也就是说结构体整体占用空间将是4的整数倍)
  int16u CRCFM;
}Glo_T;

出0入0汤圆

发表于 2010-10-28 10:19:38 | 显示全部楼层
typedef struct
{
   …………
  int32u Ba;     //32位变量,结构体整体对齐方式是4字节(也就是说结构体整体占用空间将是4的整数倍)
  int16u CRCFM;
}Glo_T;

出0入0汤圆

发表于 2010-10-28 10:22:24 | 显示全部楼层
32位,结构体默认32位整体对齐。
(结构体整体占用存储空间是4的整数倍)。

(8为机移植到16位,32位都要考虑的问题)

一般可以用编译器扩展关键字,或者预处理,改变结构体对齐方式,可以按单字节对齐。

出0入0汤圆

 楼主| 发表于 2010-10-28 10:33:32 | 显示全部楼层
回复【11楼】stm32w  

一般可以用编译器扩展关键字,或者预处理,改变结构体对齐方式,可以按单字节对齐。
-----------------------------------------------------------------------

请问这个具体如何实现?不大清楚。谢谢。

出0入162汤圆

发表于 2010-10-28 10:35:27 | 显示全部楼层
结构定义时加__packed关键字,让结构按字节对齐

否则按4字节对齐的

出0入0汤圆

 楼主| 发表于 2010-10-28 10:46:25 | 显示全部楼层
typedef        struct       
{
        int16u        ChaPa_Z;
        int16u        ChaPa_K;
        int16u        JingPa_Z;
        int16u        JingPa_K;
        int16u        Temp_Z;
        int16u        Temp_K;
        int16u        Kv;
        int16u        Kp;
        int32u        Ba;
        int16u        CRCFM;
}__packed        Glo_T ;//这样好了。

出0入0汤圆

发表于 2014-7-18 13:36:05 | 显示全部楼层
Adrian 发表于 2010-10-28 10:46
typedef        struct        
{
        int16u        ChaPa_Z;

我也遇到这样的问题,解决办法应该是这个样子的:


#pragma pack (1) /*指定按1字节对齐*/  
struct D
{      
char b;     
int a;     
short c;  
};  
#pragma pack () /*取消指定对齐,恢复缺省对齐*/



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2014-7-18 14:53:45 | 显示全部楼层
对于32位的单片机千万不要用pack压缩,严重影响效率
一般都是浪费几个字节

出0入0汤圆

发表于 2014-7-18 14:57:13 | 显示全部楼层
请参考http://www.amobbs.com/thread-5547367-1-1.html

出0入0汤圆

发表于 2014-7-18 15:18:30 | 显示全部楼层
本帖最后由 xf331785508 于 2014-7-18 15:21 编辑

Comparisons of an unpacked struct, a __packed struct, and a struct with individually __packed fields, and of a __packed struct and a #pragma packed struct
ARM Compiler toolchain v5.02 for µVision Using the Compiler   

Home > Compiler Coding Practices > Comparisons of an unpacked struct, a __packed struct, and a struct with individually __packed fields, and of a __packed struct and a #pragma packed struct

Comparisons of an unpacked struct, a __packed struct, and a struct with individually __packed fields, and of a __packed struct and a #pragma packed struct
These comparisons illustrate the differences between the methods of packing structures.

Comparison of an unpacked struct, a __packed struct, and a struct with individually __packed fields
The differences between not packing a struct, packing an entire struct, and packing individual fields of a struct are illustrated by the three implementations of a struct shown in Table 12.

Table 12. C code for an unpacked struct, a packed struct, and a struct with individually packed fields

  1. //Unpacked struct  
  2. struct foo
  3. {
  4.     char one;
  5.     short two;
  6.     char three;
  7.     int four;
  8. } c;
  9. //__packed struct
  10. __packed struct foo
  11. {
  12.     char one;
  13.     short two;
  14.     char three;
  15.     int four;
  16. } c;

  17. //__packed fields
  18. struct foo
  19. {
  20.     char one;
  21.     __packed short two;
  22.     char three;
  23.     int four;
  24. } c;
复制代码




In the first implementation, the struct is not packed. In the second implementation, the entire structure is qualified as __packed. In the third implementation, the __packed attribute is removed from the structure and the individual field that is not naturally aligned is declared as __packed.

Table 13 shows the corresponding disassembly of the machine code produced by the compiler for each of the sample implementations of Table 12, where the C code for each implementation has been compiled using the option -O2.

Table 13. Disassembly for an unpacked struct, a packed struct, and a struct with individually packed fields

  1. Unpacked struct  
  2. ; r0 contains address of c
  3. ; char one
  4. LDRB    r1, [r0, #0]
  5. ; short two
  6. LDRSH   r2, [r0, #2]
  7. ; char three
  8. LDRB    r3, [r0, #4]
  9. ; int four
  10. LDR     r12, [r0, #8]

  11. __packed struct
  12. ; r0 contains address of c
  13. ; char one
  14. LDRB  r1, [r0, #0]
  15. ; short two
  16. LDRB  r2, [r0, #1]
  17. LDRSB r12, [r0, #2]
  18. ORR   r2, r12, r2, LSL #8
  19. ; char three
  20. LDRB  r3, [r0, #3]
  21. ; int four
  22. ADD   r0, r0, #4
  23. BL    __aeabi_uread4

  24. __packed fields
  25. ; r0 contains address of c
  26. ; char one
  27. LDRB  r1, [r0, #0]
  28. ; short two
  29. LDRB  r2, [r0, #1]
  30. LDRSB r12, [r0, #2]
  31. ORR   r2, r12, r2, LSL #8
  32. ; char three
  33. LDRB  r3, [r0, #3]
  34. ; int four
  35. LDR   r12, [r0, #4]
复制代码


出0入0汤圆

发表于 2015-8-21 09:55:04 | 显示全部楼层
好贴,我之前也遇到了此类似问题。

出0入0汤圆

发表于 2015-8-21 10:29:46 | 显示全部楼层
4字节对齐,编译器位数有关

出0入10汤圆

发表于 2015-8-22 09:07:18 | 显示全部楼层
把int32u  那行和下面那行换个位试试。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 10:22

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表