# Pragma pack

Source: Internet
Author: User

You don't need to be clear about such complicated things in Chinese. It is written in English and also in Mars.

For some current processors, the memory address of your data must be align. Even if it is not, if it is align, the running speed will be improved. Although alignment will produce additional memory space, it is worth the improvement compared with this speed.

Alignment is the alignment parameter.
Value ). Valid values include 1, 2, 4, 6, 16 ,...... And 8192.

How to align? The compiler helps you with this.

How to set the alignment of the compiler? Use# Pragma pack (n
)
And_ Declspec (align (#)).

Based on the two, how does the compiler work? This is what we will talk about next.

However, it is clear that the following content is based on msdn, online articles, and running results, and cannot be ensured to be correct. We recommend that you manually perform several experiments to verify it.

# Pragma pack (N)

Let's just give an example to illustrate that N is used in this case.

# Pragma pack (4)

Struct

{

Char
A;

Short
B;

Char
C;

};

Allocation Method:

Ü A is the first, accounting for [0]

Ü short is 2, 2 is smaller than N = 4 and is aligned with 2. That is, B's address must be a multiple of 2. Therefore, [1] is not used. B occupies [2] and [3].

U char is smaller than 1, 1, and n = 4. It is aligned with 1, that is, C occupies [4].

Ü
After calculating the data members, a total of 5b is returned. Finally, because the largest element in struct a is the short of 2B, 2 is smaller than N = 4, so the size of a can be divided by 2, so the final result is 6.

Ü the entire struct memory allocation is: A occupies [0], B occupies [2] [3], C occupies [4], and [1] [5.

 

In msdn:

"The alignment of a member (doesn't the first one) will be on
Boundary that is either a multiple of N or a multiple of the size
Of the member, whichever is smaller ."

Translated into Chinese, that is:

"Except for the first data member in the struct, the address of the other data member must be a small multiple of its own size or alignment parameter ."

Expressed in mathematical expressions:

Alignedvalue = min (sizeof (datamember), n)

Datamemberaddr = alignedvalue * I, I belong to z *, and I is the smallest z * that can make datamemberaddr legal (that is, ensure that it does not go to the address of the previous data member *.

 

If you are confused by these definitions, you can ignore them, as long as you remember the allocation method.

According to this theory, if we change the sequence of data members of a, what will happen?

For example:

# Pragma pack (4)

Struct

{

Char
A;

Char
C;

Short
B;

};

Allocation Method:

Ü A is the first, accounting for [0]

U char is 1, 1 smaller than N = 4, alignment with 1, so c occupies [1]

Ü short is 2 to 2 smaller than N = 4. Use 2 alignment, that is, B occupies [2] [3].

Ü
After calculating the data members, a total of 4B is performed. Finally, because the largest element in struct a is the short of 2B, 2 is smaller than N = 4, so the size of a can be divided by 2, so the final result is 4.

 

It's strange that changing the sequence of data members can change the size of the struct.

 

Note:

Ü a ao;
Sizeof (AO. A) is 1, sizeof (AO. B) is 2.

Ü if struct B contains an object M_a,
Struct B
{
...
A M_a;
...
}
Then, the M_a alignment parameter is the size of the largest data type in a (here is short 2) and smaller in N. If this alignment parameter is the largest in B, the size of B is also related to this alignment parameter.

 

_ Declspec (align (#))

_ Declspec (align (#) is closely related to # pragma pack (n.

When a variable or struct is affected by both, the former has a higher priority.

The Member Address is determined by the former and the latter. It is either a multiple of the former, a multiple of the latter, or a multiple of the member size, and the minimum value.

The final size of a struct is related to the former, which is either a multiple of the former or a multiple of the maximum offset of the struct.

To calculate the final result, you must know the values or default values of the two.

 

Convention, directly to the example:

# Pragma pack (push, 4)

_ Declspec (align (32) struct d

{

Int
I1;

Double
D1;

Int
I2;

Int
I3;

};

Ü I1 is the first, accounting for [0] [1] [2] [3]

U double is 8 to 8 larger than N = 4, with 4 alignment, therefore, D1 accounts for [4] [5] [6] [7] [8] [9] [10] [11].

Ü the int size is 2 to 2 smaller than N = 4 and is aligned with 2. That is, I2 occupies [12] [13] [14] [15].

U int is 2 to 2 smaller than N = 4, with 2 alignment, that is, I3 occupies [16] [17] [18] [19]

Ü until now, the calculation method is exactly the same as the above examples. It is calculated that the total number of members is 20 B, and the maximum offset of the members is 4.

Ü when the final size of the struct is calculated, because the struct accepts _ declspec (align (32)
) Impact, compared to 4 and #, 4 ratio # = 32 is small, fill with 32, so the final size should be greater than 20b's smallest 32 multiples, that is, 32B.

 

Now define e with d

_ Declspec (align (16) struct E

{

Int
I1;

D
M_d;

Int
I2;

};

 

# Pragma pack (POP)

Ü I1 is the first, accounting for [0] [1] [2] [3]

Ü m_d is subject to the previous _ declspec (align (32)
) Influence. The priority is high. The offset is 32, and 32 is used for alignment. Therefore, D1 occupies [32]-[63].

Ü the int size is 2 to 2 smaller than N = 4 and is aligned with 2. That is, I2 occupies [64] [65] [66] [67].

Ü calculate that the total number of members is 68b, and the maximum offset of the members is 32.

Ü when the final size of the struct is calculated, because the struct accepts _ declspec (align (16)
) Impact, compared to 32 and #, 32 ratio # = 16, supplemented with 32, so the final size should be greater than the minimum 32 multiples of 68b, that is, 96b.

 

Msdn:

"The sizeof value for any structure is the offset of the final
Member, plus that member's size, rounded up to the nearest multiple
Of the largest member alignment value or the whole structure
Alignment value, whichever is greater ."

Chinese:

"The result of sizeof is that the last member variable in the struct is added with its size, plus a fill capacity (padding ), the fill size is the largest alignment parameter of the member variable or a multiple of the alignment parameters of the entire structure. Which of the following alignment parameters determines which alignment parameters are greater"

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: info-contact@alibabacloud.com 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.