#pragma pack(2) 的含义
有什么作用
#pragma pack(2)
?这是什么意思?
What does
#pragma pack(2)
do? What does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有什么作用
#pragma pack(2)
?这是什么意思?
What does
#pragma pack(2)
do? What does it mean?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
这意味着结构、联合或类按 2 个字节对齐。这意味着,以下结构将在内存中使用 3 个字节而不是 2 个:
以下结构将使用 4 个字节:
更多信息: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:
The following will use 4 bytes:
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 ;-)
这是一个特定于 Visual-Studio 的
pragma
指令,用于更改struct
中成员的对齐方式。完整的详细信息可以在 MSDN 上找到< /a>,但其要点是它允许您自定义在struct
的元素之间放置多少填充。将物品包装得更紧会占用更少的空间,但可能会带来对齐问题。This is a Visual-Studio-specific
pragma
directive that changes how members instruct
s 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 astruct
. Packing things in tighter uses less space but can give you alignment issues.这意味着编译器应该打包 struct/class/union 成员,以便它们在 2 字节边界上对齐。
使用 pack(2) 时,结构将具有:
注意,所有编译指示都是特定于编译器的 - 不过,该编译指示适用于 VC 和 gcc。
It means that the compiler should pack struct/class/union members so that they're aligned on a 2-byte boundary.
With pack(2), the struct will have:
Note that all pragmas are compiler specific - this one works on both VC and gcc though.
这意味着在结构体第一个成员后面的编译指示之后声明的结构体、类或联合的每个成员都以成员类型大小或 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.