Structure Packing
来源:互联网 发布:js实现a标签跳转 编辑:程序博客网 时间:2024/05/17 06:58
Structure Packing
Posted by: Russ Keldorph
Structure packing is an extenstion to the C++ language supported by many compilers, including Microsoft's. In our language, packing is achieved with either #pragma pack(N), which allows fine-grained control over structure packing, or the -ZpN compiler switch, which is equivalent to adding #pragma pack(N) to the beginning of a translation unit.
Microsoft's packing semantics are best explained in the greater context of alignment, so that's how I'll attempt it.
#pragma pack(N), places a limit of N on the alignment of structure members relative to the beginning of their parent structure. The packing alignment can be changed at any point during a structure's definition so that some members can have a different limit than others within the same structure.
Therefore, the formal alignment rules are:
- The natural alignment of a scalar data type (char, int, double, etc.) is its size.
- The natural alignment of an aggregate data type (array or struct) is the alignment of its member(s) with the most restrictive (greatest) alignment.
- The alignment (A) of a structure member (scalar or aggregate) is the minimum of its natural alignment (NA) and the current packing alignment (PA) unless __declspec(align(#)) (DA) is used. It that case, __declspec(align(#)) sets the minimum alignment, i.e. A = max(DA, min(NA, PA)).
- The offset of the first member from the beginning of its parent structure is zero (0).
- The offset of the second and each subsequent member from the beginning of its parent structure is the smallest multiple of its alignment greater than or equal to the offset of the end of the previous member. Padding is inserted before the member if necessary.
- The size of an aggregate data type is the smallest multiple of its alignment greater than or equal to the offset of the end of its last member. Padding is appended after the last member if necessary.
Note the distiction between natural alignment and alignment. Natural alignment is an attribute of a type, but the actual alignment of a piece of data is affected by more than its type. Also note that packing can cause data to become misaligned, i.e not naturally aligned. Compilers have to be very careful about alignment if packing is involved because some processors cannot deal with misaligned data in the same way they deal with naturally aligned data. My next post will cover that topic in more detail.
Since I'd like to keep this post as short as possible, I'll leave examples up to the reader. I encourage you to post sample structures and use the rules to predict the alignment and offsets of members and the structures themselves.
- Structure Packing
- Structure Packing
- Structure Member Alignment, Padding and Data Packing
- The Lost Art of C Structure Packing
- Structure Member Alignment, Padding and Data Packing
- The Lost Art of C Structure Packing
- The Lost Art of C Structure Packing - Eric S. Raymond
- structure
- Packing Rectangles
- Packing Rectangles
- Packing Rectangles
- Packing Rectangles
- Packing Rectangles
- Packing & Bag Sealer Series
- Bin Packing pku 2782
- USACO--Packing Rectangles
- 102 - Ecological Bin Packing
- USACO 1.4-Packing Rectangles
- 怎样判断一个字符是否为中文字
- 如何在 Microsoft Visual C# .NET 中实现 Microsoft Excel 自动化
- 使用Server.Transfer在两个ASPX页面间传递参数
- 通过Web Services上传和下载文件
- 什么是VoIP
- Structure Packing
- core jsf第四章
- ant使用教程
- 用VB.NET结合Excel设计统计生产报表
- Generic Class和MFC Class和Form Class有何区别?
- SSI-SHTML教程
- ADO.NET 的最佳实践技巧
- Windows服务编写原理及探讨1
- vx