Understanding # pragma pack (N)

Source: Internet
Author: User

I read a book in the morning and saw # pragma pack (N). I don't know much about it. I want to study it decisively. I 'd like to leave some experiences for you to discuss and discuss.

 

For example, the general question is how much sizeof (S1) is:

# Pragma pack (n) <br/> typedef struct _ S1 <br/>{< br/> char M1; <br/> short m2; <br/> int M4; <br/> char m3 [3]; <br/>} S1; <br/> # pragma pack ()

 

1. When n is greater than the length of the longest Data Type of the struct (such as the int In the example), the N will be declared to have no effect.

2. during the calculation of the size and completion, the alignment is performed based on the small values in the Data Type and N value. For example, if char is 1 and N is 4, alignment is performed based on 1, when a new member is added, the starting offset of the member must be an integer multiple of its alignment value. well, It's abstract. For example, suppose n = 4, for example, S1, the first is M1, M1 is Char, one byte, which is aligned with one byte, when m2 is added, M2 is short and two bytes. At this time, the starting offset of M2 is 2 and the offset is counted from 0. in this case, you need to add a byte [M1, 00, (M2, m2)], and then add M4, Which is exactly four bytes, and the starting offset is 4, which is good, the next three char strings are 4 aligned, so we need to add one more byte. The total length of the struct is 12. if n = 8, then the length of 8> int, so the struct is aligned in 4 bytes. if n = 1, no alignment is required. The length of the struct is 10.

3. when a struct contains a struct member, the struct member is aligned according to its length. For example, I now have a S2 structure with a member of the S1 structure, if n = 1 when I declare S1, S1 participates in alignment with a length of 10 in S2. If n = 4 in S1 is declared, S1 is aligned with a length of 12 in S2. it should be noted that the members of the S1 struct are aligned in the form of several bytes, depending on who is smaller than the int value of S1. this should be used to determine the memory location of internal members.

 

I think the analysis is good for you.

----------------------------------------------- I am a split line --------------------------------------------------

# Pragma pack (8) <br/> struct S1 <br/>{< br/> short a; <br/> long B; <br/> }; </P> <p> struct S2 <br/>{< br/> char C; <br/> struct S1 D; <br/> long E; <br/>}; <br/> # pragma pack ()

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

 

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.