Alignment of struct didn't work with #pragma pack

本小妞迷上赌 提交于 2019-12-01 13:39:34

The fault packing for most compilers I use is that objects align on their own size. The default packing for your struct would insert padding after the char and before the first int, to place that int on a 4 byte boundary. Obviously this is the behaviour you are seeing.

The code I use on Visual Studio to achieve packing looks like this.

#pragma pack(push,1)
struct a {
  char b;
  int c;
  int d[100];
};
#pragma pack(pop)

It removes the padding, aligning the int on byte 1.

If I get some time I'll check it on a couple of versions of VS to confirm that, but my current code works exactly like this. This is how it's done, so the only question is why it isn't working for you.

EDIT: sizeof(a) is 405 as expected. Offset of b is 1. VS2012. Gcc is the same.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!