打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
#pragma pack()
userphoto

2014.03.27

关注
 (Very ) Basically the compiler unless otherwise directed will line up structure members on
2 byte or 4 byte boundaries - this makes it easier and faster for the processor to handle.
So the structure contains secret padding bytes to make this happen.

The pragma pack directive (in MS Compiler) allows you to change this alignment scheme.

Some things (particularly in relation to hardware) do not have the luxury to waste bytes like
this and they send their data in an exact fit.
This means that it is not wise to read data from a hardware device directly into a normal structure.

If you have want to read data that is an exact fit into a structure - you can tell the compiler
to make the structure an exact fit like this (microsoft compiler);
1
2
3
4
5
6
7
8
#pragma pack(push, 1) // exact fit - no paddingstruct MyStruct{  char b;   int a;   int array[2];};#pragma pack(pop) //back to whatever the previous packing mode was 


Without the pragma directive, the size of the structure is 16 bytes - with the packing of 1 - the size is 13 bytes 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
#pragma pack(push) 和#pragma pack(pop) 以及#pragma pack()
Jserv's blog: C99 的 offsetof macro
Lesson 23. Pattern 15. Growth of structures' sizes...
Debugging with conditional compiler directive
我用Delphi一年[论使用Inc文件的一点心得]
Introduction to Containerization – Automated packing process in Microsoft Dynamics AX 2012 R3 – Supply Chain Management in Dynamics AX
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服