C Language Memory alignment details (2)

Source: Internet
Author: User

Next:C language Memory alignment detailed (1)

VC storage of the structure of the special processing does increase the speed of the CPU storage variables, but sometimes it brings some trouble, we also block out the default alignment of variables, we can set the alignment of variables. The #pragma pack (n) is provided in the VC to set the variable to n-byte alignment. N-byte alignment means that the offset of the starting address at which the variable is stored is in two cases:

First, if n is greater than or equal to the number of bytes occupied by the variable, then the offset must satisfy the default alignment;

Second, if n is less than the number of bytes occupied by the type of the variable, then the offset is a multiple of n and does not satisfy the default alignment.

The total size of the structure also has a constraint, in the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the number of spaces occupied by the variable occupying the largest space, otherwise it must be a multiple of n. The following examples illustrate their usage:

#pragma pack (push)       //#pragma pack (4)          //struct  test{     char  M1;      Double M4;      int  #pragma pack (POP)        //

The size of the above structure is 16, the following analysis of its storage, the first to allocate space for M1, its offset is 0, to meet our own set of alignment (4-byte alignment),M1 occupies 1 bytes. It then begins allocating space for the M4, where the offset is 1 and 3 bytes are required so that the offset satisfies the multiple of n=4 (because sizeof (double) is greater than N), M4 occupies 8 bytes. The M3 is then allocated space, at which point the offset is 12, satisfies the multiple of 4, andM3 occupies 4 bytes. At this time, all member variables have been allocated space, 4+8+4=16 bytes are allocated, which satisfies a multiple of n. If you change the #pragma pack (4) above to #pragma pack (16), then we can get the size of the structure to 24.

Let's look at the following example:

#pragma Pack (8)struct  s1{    Char  A;     Long b;}; struct S2 {    char  C;     struct S1 D;     Long Long e;}; #pragma pack ()

Member alignment has an important condition in which each member is aligned individually. That is, each member is aligned in its own way.

That is, although the above specifies a 8-byte alignment, not all members are aligned with 8-byte alignment. The rule of alignment is that each member is aligned by its type (usually the size of this type) and the alignment parameter is specified (this is 8 bytes) The smaller one is aligned. And the length of the structure must be an integer multiple of all the alignment parameters used, not enough to fill the empty bytes.

In S1, member A is 1 bytes by default by 1-byte alignment, the alignment parameter is 8, the two values are 1,a by 1 bytes, the member B is 4 bytes, by default is 4 byte aligned, then by 4 byte alignment, so sizeof (S1) should be 8;

S2, C and a in S1, as in 1-byte alignment, and D is a struct, it is 8 bytes, what is it aligned? For a struct, its default alignment is the largest of the alignment parameters used by all its members, and S1 is 4. Therefore, member D is aligned by 4 bytes. Member E is 8 bytes , it is the default by 8 byte alignment, and the same as specified, so it is on the boundary to 8 bytes, at this point, has used 12 bytes, so added 4 bytes of empty, starting from the 16th byte to place the member E. At this point, the length is 24, which can already be divisible by 8 (Member E in 8-byte alignment). sizeof (S2) is 24 bytes.

Here are three important points:

1. Each member is aligned in its own way and can minimize the length.

2. The default alignment of a complex type, such as a structure, is the alignment of its longest member, so that the length can be minimized when the member is a complex type.

3. The length of the alignment must be an integer multiple of the largest alignment parameter in the member, so that each item is aligned to the boundary when the array is processed.

C Language Memory alignment details (2)

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.