是什么禁用了#pragma pack(push)?

发布于 2024-09-07 02:17:06 字数 529 浏览 2 评论 0原文

我有一个使用 #pragma pack(push,8) 的代码,但它似乎没有以某种方式生效,但我无法弄清楚是什么导致了这个问题。

例如,看看下面的代码。

#include <windows.h>
#include <stdio.h>

#pragma pack(push, 8)

typedef struct _MY_DATA {
 LARGE_INTEGER a;
 LARGE_INTEGER b;
 ULONG count;
} MY_DATA;

#pragma pack(show)
#pragma pack(pop)

int main()
{
 MY_DATA data;

 printf("data size:%d\n", sizeof(data));

 return 0;
}

这将返回“数据大小:24”,但我在 Visual Studio 管理的其他应用程序中使用相同的代码,并且我得到“数据大小:20”。

所以我假设它与某些设置有关,但无法弄清楚。如果有人能给我一些提示,我将非常感激。谢谢。

I have a code that uses #pragma pack(push,8) but it does not seem to take effect somehow but I can't figure out what's causing this problem.

For example, look at the following code.

#include <windows.h>
#include <stdio.h>

#pragma pack(push, 8)

typedef struct _MY_DATA {
 LARGE_INTEGER a;
 LARGE_INTEGER b;
 ULONG count;
} MY_DATA;

#pragma pack(show)
#pragma pack(pop)

int main()
{
 MY_DATA data;

 printf("data size:%d\n", sizeof(data));

 return 0;
}

This would return "data size:24" but I use the same code in other application that is managed by Visual Studio and there I am getting "data size:20".

So I am assuming it has to do with some settings but could not figure out. I will really appreciate it if anybody could give me some hint. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鸠书 2024-09-14 02:17:06

项目默认设置的优先级可以高于编译指示 - 请参阅这篇 MSDN 文章。

结构打包与编译器对齐行为相互作用,如下所示。

  • 如果包装大小设置为等于或大于默认对齐方式,则包装大小将被忽略。
  • 如果将 packsize 设置为小于默认对齐方式,编译器将根据 packsize 值进行对齐。

The project default setting can have higher precedence than the pragma - see this MSDN article.

Structure packing interacts with compiler alignment behavior as follows.

  • If the packsize is set equal to or greater than the default alignment, the packsize is ignored.
  • If the packsize is set smaller than the default alignment, the compiler aligns according to the packsize value.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文