#pragma pack(2) 的含义

发布于 2024-10-26 09:11:33 字数 105 浏览 0 评论 0原文

有什么作用

#pragma pack(2)

?这是什么意思?

What does

#pragma pack(2)

do? What does it mean?

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

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

发布评论

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

评论(4

狼性发作 2024-11-02 09:11:33

这意味着结构、联合或类按 2 个字节对齐。这意味着,以下结构将在内存中使用 3 个字节而不是 2 个:

struct MyStruct
{
    char Field1;
    char Field2;
};

以下结构将使用 4 个字节:

struct MyStruct
{
    WORD Field1;
    WORD Field2;
};

更多信息:http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx。重要提示:阅读问题并仅在您知道自己在做什么并且需要时才使用它;-)

It means that structs, unions or classes are aligned by 2 bytes. That means, the following struct will use 3 bytes in memory instead of 2:

struct MyStruct
{
    char Field1;
    char Field2;
};

The following will use 4 bytes:

struct MyStruct
{
    WORD Field1;
    WORD Field2;
};

More here: http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx. Important: Read about the problems and use it only if you know what you are doing and you need it ;-)

放手` 2024-11-02 09:11:33

这是一个特定于 Visual-Studio 的 pragma 指令,用于更改 struct 中成员的对齐方式。完整的详细信息可以在 MSDN 上找到< /a>,但其要点是它允许您自定义在 struct 的元素之间放置多少填充。将物品包装得更紧会占用更少的空间,但可能会带来对齐问题。

This is a Visual-Studio-specific pragma directive that changes how members in structs are aligned. The full details can be found here on the MSDN, but the gist of it is that it allows you to customize how much padding is placed in-between the elements of a struct. Packing things in tighter uses less space but can give you alignment issues.

节枝 2024-11-02 09:11:33

这意味着编译器应该打包 struct/class/union 成员,以便它们在 2 字节边界上对齐。

struct Foo {
  char c1;
  int i1;
};

使用 pack(2) 时,结构将具有:

  • 1 个字节用于 char
  • 1 个未使用的字节,填充
  • 4 个字节(假设为 32 位)用于 int 请

注意,所有编译指示都是特定于编译器的 - 不过,该编译指示适用于 VC 和 gcc。

It means that the compiler should pack struct/class/union members so that they're aligned on a 2-byte boundary.

struct Foo {
  char c1;
  int i1;
};

With pack(2), the struct will have:

  • 1 byte for the char
  • 1 unused byte, padding
  • 4 bytes (assuming 32-bit) for the int

Note that all pragmas are compiler specific - this one works on both VC and gcc though.

不疑不惑不回忆 2024-11-02 09:11:33

这意味着在结构体第一个成员后面的编译指示之后声明的结构体、类或联合的每个成员都以成员类型大小或 2 字节边界(以较小者为准)的倍数存储。

#pragma pack(n) 将影响其后面的结构、类和联合的大小。

如果您在文件级别使用它,那么在更改打包对齐之前保存它可能是一个好主意,并在您想要应用新打包对齐的声明结束时将其恢复到之前的值。

当然,您应该查看编译器的文档。

对于 MS VS 6.0:请参阅此处

Well it means that each member of a structure, class or union declared after the pragma that follows the first member of the structure is stored at a multiple of either the size of the member type or 2 byte boundary, whichever is smalle.

#pragma pack(n) will affect the size of the structures, classes and unions that follow it.

if you use it a file level it is probably a good ideea to save the packing alignment before changing it and restoring it back to its previous value when the declarations you want to apply the new packing alignment end.

And of course you should look at in the documentation for your compiler.

For MS VS 6.0: see Here.

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