Memory alignment Issues

Source: Internet
Author: User

Alignment issues for basic data types:

Where variables are stored in memory generally requires natural alignment. The so-called natural alignment , is that the basic data type of the variable can not be stored simply in memory arbitrary position, but its starting address must meet can be divisible by their size. For example, 32-bit platforms, the address of int and pointer type variables should be divisible by 4, the address of the short type variable should be divisible by 2, char and bool because of 1 bytes, so the equivalent of no alignment requirements.

Alignment issues for composite data types:

In a composite data type, the members must satisfy the natural alignment rules, where the playhead is based on the relative address. It also takes into account the possibility of using arrays of that type, so the compiler inserts a certain number of bytes at the end of the last variable (how many bytes are inserted to the maximum base type contained in the composite type) to ensure that even if the type array is used, the access efficiency is not affected as much as possible (the elements in the general array are stored continuously, If a composite type occupies a space that satisfies a multiple of the maximum base type it contains, even with a compound type array, the next composite type object in the array can be accessed with greater efficiency. Here is an example program:

1 structA2 {3         BOOLA;4         intb;5         BOOLC;6         DoubleD;7         BOOLe;8 9 };Ten  One intMain () A { -         structA sa; -cout <<sizeof(SA) <<Endl; thecout <<"&SA.A ="<< &AMP;SA.A <<Endl; -cout <<"&sa.b ="<< &sa.b <<Endl; -cout <<"&SA.C ="<< &AMP;SA.C <<Endl; -cout <<"&SA.D ="<< &AMP;SA.D <<Endl; +cout <<"&SA.E ="<< &AMP;SA.E <<Endl; -  +         return 0; A } at  - // + - //&sa = 0x22fee0 - //&sa.a = 0x22fee0 - //&sa.b = 0x22fee4 - //&sa.c = 0x22fee8 in //&SA.D = 0x22fef0 - //&SA.E = 0x22fef8

Analysis:

The memory layout of the variable SA is as follows:

First the address of a and the structure variable SA address are the same.

A occupies 1 bytes, the address is 0x22fee0;

B occupies 4 bytes, but in order to satisfy the principle of natural alignment, its relative address must be a multiple of 4, so the smallest can use address is 0x22fee4;

C occupies 1 bytes, and any address satisfies the natural alignment principle, so it is stored next to B;

D occupies 8 bytes, its starting address must meet a multiple of 8, so the minimum available address is 0x22fef0;

E takes 1 bytes, but why does it then populate with 7 extra bytes? This is set by the compiler to satisfy the array alignment of struct a types. Imagine that if you do not populate this extra 7 bytes, then if you define an array of struct a type, then the storage address of the next element in the array must be OX22FEF1, then it is not naturally aligned, which will inevitably affect the access efficiency of the array. Therefore, the storage address of an object of a composite type must be an integer multiple of the bytes of the largest type contained therein (assuming that the largest type contained in it x Byte, then the conforming type must satisfy the X byte alignment).

When a composite type object is created in memory, the address of each member is a relative address (relative to the starting address of the composite type), depending on the number of offset bytes relative to the start address of the object. You can use the Offsetof macro to see the number of offset bytes for different members relative to the start address of a composite type:

1cout <<"Offsetof (A, a) ="<< Offsetof (A, a) <<Endl;2cout <<"Offsetof (A, b) ="<< Offsetof (A, b) <<Endl;3cout <<"Offsetof (A, c) ="<< Offsetof (A, c) <<Endl;4cout <<"Offsetof (A, d) ="<< Offsetof (A, D) <<Endl;5cout <<"Offsetof (A, e) ="<< Offsetof (A, E) <<Endl;6 7 //Offsetof (A, a) = 08 //Offsetof (A, b) = 49 //Offsetof (A, c) = 8Ten //Offsetof (A, d) = One //Offsetof (A, e) =

The user can also specify how members are to be used for this object type, which can be implemented using the #pragma compilation directives. In practice, it is a good idea to explicitly specify the alignment for each composite data type (but note that after you specify the alignment, although you can improve program efficiency to some extent, but not portable), the following is the sample code:

As you can see from the above procedure, the GCC compiler defaults to 8-byte alignment.

Because of the natural alignment and composite type alignment constraints in the storage of variables, when designing a composite type, we try to arrange the larger space-occupying members to the front so that even if the alignment is required, it is mainly small-type members, which helps to conserve storage space , such as:

1 #pragmaPack (push, 8)2 structA3 {4         DoubleD;5         intb;6         BOOLA;7         BOOLC;8         BOOLe;9 };Ten #pragmaPack (POP) One //This arrangement of data members, the structure of the space to become 16 bytes, a full reduction of half. 

In summary, we can see that the factors affecting the actual size and access efficiency of the object include: data member type, declaration order, alignment . at the same time, different alignments also affect semantic consistency and binary compatibility between interfaces, such as when an application contains several modules, and the alignment of different modules is different, if the composite data type shared between the modules does not explicitly specify alignment, The probability of a program error is greatly increased, because different modules interpret the same shared data differently.

Memory alignment Issues

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.