Visual C 中的结构成员对齐 2008年

发布于 2024-07-19 10:52:53 字数 117 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖 2024-07-26 10:52:53

对于真正特定的结构对齐,您可以摆弄填充字节,

因此在各个字段之间添加一些虚拟字节,直到对齐满足您的需要。

示例:

结构示例
{
无符号短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 ;^)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文