# Role of Pragma pack (N)

Source: Internet
Author: User

In the C language, the structure is a composite data type, and its components can be both variables of basic data types (such as int, long, float, and so on, it can also be a data unit of a composite data type (such as an array, structure, and union. In the structure, the compiler allocates space for each member of the structure based on its natural alignment condition. Each member is stored in the memory in the declared order. The address of the first member is the same as that of the entire structure.

For example, the following structure shows the allocation of member spaces:
Struct Test
{
Char x1;
Short X2;
Float X3;
Char X4;
};

The offset address of the first Member X1 In the constructor is 0, occupying 1st bytes. The second member X2 is of the short type, and its starting address must be two byte pairs. Therefore, the compiler fills a Null Byte between X2 and X1. The third member X3 and fourth member X4 of the structure exactly fall on their natural peer address, and no additional padding Section is required before them. In the test structure, the X3 member requires a 4-byte bounded boundary and is the maximum boundary unit required by all the members of the structure. Therefore, the natural boundary condition of the test structure is 4 bytes, the compiler fills in three NULL bytes after Member X4. The entire structure occupies 12 bytes of space.

Change the default byte alignment of the C Compiler
By default, the C compiler allocates space for each variable or data unit based on its natural limitations. Generally, you can use the following method to change the default peer condition:
· With the pseudo command # pragma pack (n), the C compiler will align according to n Bytes.
· Use the pseudo command # pragma pack () to cancel the custom byte alignment.

In addition, the following method is provided:
· _ Attribute (aligned (N) to align the structure members to the natural boundary of n Bytes. If the length of a member in the structure is greater than N, the maximum member length is used for alignment.
· _ Attribute _ (packed): cancels the optimization alignment of the structure during compilation and alignment according to the actual number of bytes occupied.

The first method above n = 1, 2, 4, 8, 16... is more common.

Below isCsdn ForumHot questions:

Interview Questions from companies such as Intel and Microsoft

# Pragma pack (8)

Struct S1 {
Short;
Long B;
};

Struct S2 {
Char C;
S1 D;
Long long E;
};

# Pragma pack ()

Question
1. sizeof (S2) =?
2. How many bytes are left behind C in S2 and then D?

The result is as follows:

The sizeof (S2) result is 24.
An important condition for member alignment is that each member is aligned separately. That is, each member is aligned in its own way.
That is to say, although alignment by 8 bytes is specified above, not all Members are alignment by 8 bytes. the alignment rule is that each member has a smaller alignment according to its type alignment parameter (usually the size of this type) and the specified alignment parameter (8 bytes here. in addition, the length of the structure must be an integer multiple of all alignment parameters used.
In S1, member A is 1 byte, which is aligned by 1 byte by default, and the specified alignment parameter is 8. Among the two values, 1 and a are aligned by 1 byte. Member B is 4 bytes, the default value is 4-byte alignment. In this case, it is 4-byte alignment, so sizeof (S1) should be 8;
In S2, C is the same as a in S1. It is aligned by 1 byte, while D is a structure. It is 8 bytes. What is its alignment? For the structure, its default alignment is the largest of all its members using alignment parameters, and S1 is 4. therefore, member D is aligned in 4 bytes. the member e is 8 bytes, which is aligned by 8 bytes by default. It is the same as the specified one, so it is connected to the boundary of 8 bytes. At this time, 12 bytes are used, therefore, four bytes are added, and the member e is placed starting from 16th bytes. at this time, the length is 24 and can be divisible by 8 (member e is aligned by 8 bytes. in this way, a total of 24 bytes are used.
A B
Memory layout of S1: 11 **, 1111,
C s1.a s1. B d
Memory layout of S2: 1 ***, 11 ***, 1111, *** 11111111

There are three important points:
1. Each Member is aligned in its own way, and the length can be minimized.
2. The default alignment of complex types (such as structures) is the alignment of its longest member, so that the length can be minimized when the member is a complex type.
3. The alignment length must be an integer multiple of the largest alignment parameter in the member. This ensures that each item is bounded when processing arrays.

In addition, for arrays, such:
Char A [3]; in this way, its alignment is the same as writing three char, that is, it is still aligned by 1 byte.
If you write: typedef char array3 [3];
The alignment of array3 type is still aligned by 1 byte, rather than by its length.
Whatever the type, the alignment boundary must be 1, 2, 4, 8, 16, 32, 64.

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.