Visual C 中的结构成员对齐 2008年
Visual C++ 允许您在项目的属性页中选择结构成员alignemnt。 问题是,此配置正在用于项目中的所有结构。
有没有什么方法(我猜是 VC++ 特有的)来单独设置某个结构的成员对齐方式?
Visual C++ let's you select the struct members alignemnt in the project's properties page. Problem is, this configuration is being used for all srtructs in the project.
Is there any way (VC++ specific, I'd guess) to set a certain struct's member alignment individually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://msdn.microsoft.com/en-us/库/2e70t5y1(VS.80).aspx
http://msdn.microsoft.com/en-us/library/2e70t5y1(VS.80).aspx
#pragma pack
#pragma pack
对于真正特定的结构对齐,您可以摆弄填充字节,
因此在各个字段之间添加一些虚拟字节,直到对齐满足您的需要。
示例:
结构示例
{
无符号短x;
字节虚拟1;
字节虚拟2;
字节虚拟3;
字节虚拟4;
字节虚拟5;
字节虚拟6;
无符号整型 y;
};
如果没有放置虚拟字节,则 int 可能会放置在偏移量 4 上(距结构开头的 4 个字节,而现在它已放置在偏移量 8 处)
Waring:非常特定于编译器,并且代码很糟糕练习;^)
for really specific structure alignments you can fiddle with padding bytes
So add a few dummy bytes between the various fields, until the alignment fits with your needs.
example:
struct example
{
unsigned short x;
byte dummy1;
byte dummy2;
byte dummy3;
byte dummy4;
byte dummy5;
byte dummy6;
unsigned int y;
};
if the dummy bytes wouldn't have been placed, the int would probably have been places on offset 4 (4 bytes from the beginning of the struct, while now it has been placed at offset 8)
waring: very compiler specific, and bad code practice ;^)