Snap to memory

Source: Internet
Author: User
Tags pack

Compilers on each particular platform have their own default "alignment coefficients" (also known as Zimo numbers). Programmers can change this factor by precompiling the command #pragma pack (n), where n is the "alignment factor" you want to specify, and can only be a power of 2.

The alignment rules are as follows:

1. Data member Alignment rules: Structure (struct) (or union), the first data member is placed at offset 0, and the alignment of each data member at a later time is based on the value specified by the #pragma pack and the length of the data member itself, compared to the smaller one.

2, the overall alignment rule of a struct (or union): After the data member has been aligned, the structure (or union) itself is aligned, and alignment is performed in the smaller of the value and structure (or union) of the maximum data member length specified by the #pragma pack.

3, combined with 1, 2 inference: When the n value of the #pragma pack equals or exceeds the length of all data members, the size of this n value will not produce any effect.

4. The offset of the starting address of each member variable in relation to the starting address of the structure must be a multiple of the number of bytes occupied by the type of the variable.

5. Each member variable at the time of storage in accordance with the order in the structure of the application space, in accordance with the above alignment to adjust the position, empty bytes automatically populated.

6. In order to ensure that the size of the structure is a multiple of the number of bytes in the structure (that is, the number of bytes occupied by the type that occupies the maximum space in the structure), the blank bytes are automatically populated as needed after the space is requested for the last member variable. Reference:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.