Linux Memory Alignment

Source: Internet
Author: User

In recent projects, we have been involved in "memory alignment" techniques. For most programmers, "memory alignment" should be "transparent" to them. "Memory alignment" should be the "jurisdiction" of the compiler. The compiler arranges each "data unit" in the program in the appropriate location. But one feature of C is that it is too flexible and too powerful to allow you to intervene in "memory alignment". If you want to know more about the underlying secret, "memory alignment" should not be transparent to you.

One, the reason for the memory alignment
Most of the resources are said to be:
1, platform reason (transplant reason): Not all hardware platform can access arbitrary data at any address; some hardware platforms can only fetch certain types of data at certain addresses, or throw hardware exceptions.
2. Performance reasons: Data structures (especially stacks) should be aligned as much as possible on natural boundaries. The reason is that, in order to access unaligned memory, the processor needs to make two memory accesses, while aligned memory access requires only one access.

Second, the alignment rules
Compilers on each particular platform have their own default "alignment factor" (also known as the number of Zimo). Programmers can change this factor by precompiling the command #pragma pack (n), where n is the "alignment factor" you want to specify.

Rules:
1. Data member Alignment rules: data members of a struct (struct) (or union), where the first data member is placed at offset 0, and subsequent alignment of each data member according to the value specified by the #pragma pack and the length of the data member itself, than the smaller one.
2, structure (or union) of the overall alignment rules: After the data members have completed their respective alignment, the structure (or union) itself will be aligned, alignment will follow the value specified by the #pragma pack and the structure (or union) of the maximum data member length, the smaller one.
3, combined with 1, 2 inference: When the n value of the #pragma pack equals or exceeds the length of all data members, the size of the N value will have no effect.

Third, the test
Let's prove the rule with a detailed description of the series of examples.
The compiler I'm experimenting with is GCC 4.4.6

We will use a typical struct to explain. First we define a struct:
#pragma pack (n)/* n = 1, 2, 4, 8, 16 */
struct TEST_T {
int A;
Char b;
Short C;
Char D;
};
#pragma pack (n)
First we first confirm the size of each type on the test platform and verify that the output of the two compilers is:
sizeof (char) = 1
sizeof (short) = 2
sizeof (int) = 4

Our test process is as follows: Change the alignment factor by #pragma pack (n), and then look at the value of sizeof (struct test_t).

1, 1-byte alignment (#pragma pack (1))
Output: sizeof (struct test_t) = 8
Analysis Process:
1) alignment of member data
#pragma pack (1)
struct TEST_T {
int A; /* Length 4 < 1 press 1 to align; start offset=0; storage location interval [0,3] */
Char b; /* Length 1 = 1 press 1 to align; start offset=4; storage location interval [4] */
Short C; /* Length 2 > 1 press 1 to align; start offset=5; storage location interval [5,6] */
Char D; /* Length 1 = 1 press 1 to align; start offset=7; storage location interval [7] */
};
#pragma pack ()
Total member Size =8

2) Overall alignment
Overall alignment factor = min ((max (Int,short,char), 1) = 1

2, 2-byte alignment (#pragma pack (2))
Output: sizeof (struct test_t) = 10
Analysis Process:
1) alignment of member data
#pragma pack (2)
struct TEST_T {
int A; /* Length 4 > 2 press 2 to align; start offset=0; storage location interval [0,3] */
Char b; /* Length 1 < 2 press 1 to align; start offset=4; storage location interval [4] */
Short C; /* Length 2 = 2 press 2 to align; start offset=6; storage location interval [6,7] */
Char D; /* Length 1 < 2 press 1 to align; start offset=8; storage location interval [8] */
};
#pragma pack ()
Total member Size =9

2) Overall alignment
Overall alignment factor = min ((max (Int,short,char), 2) = 2

3, 4-byte alignment (#pragma pack (4))
Output: sizeof (struct test_t) = 12
Analysis Process:
1) alignment of member data
#pragma pack (4)
struct TEST_T {
int A; /* Length 4 = 4 press 4 to align; start offset=0; storage location interval [0,3] */
Char b; /* Length 1 < 4 press 1 to align; start offset=4; storage location interval [4] */
Short C; /* Length 2 < 4 press 2 to align; start offset=6; storage location interval [6,7] */
Char D; /* Length 1 < 4 press 1 to align, start offset=8, store position interval [8], and finally fill in 3 slots to meet the total length of 4 (int is 4 bytes, 4=4) integer Times */
};
#pragma pack ()
Total member Size =9

2) Overall alignment
Overall alignment factor = min ((max (Int,short,char), 4) = 4

4, 8-byte alignment (#pragma pack (8))
Output: sizeof (struct test_t) = 12
Analysis Process:
1) alignment of member data
#pragma pack (8)
struct TEST_T {
int A; /* Length 4 < 8 press 4 to align; start offset=0; storage location interval [0,3] */
Char b; /* Length 1 < 8 press 1 to align; start offset=4; storage location interval [4] */
Short C; /* Length 2 < 8 press 2 to align; start offset=6; storage location interval [6,7] */
Char D; /* Length 1 < 8 press 1 to align; start offset=8; storage location interval [8] Finally, 3 slots are added to meet the total length of 4 (int is 4 bytes, 4<8) integer Times */
};
#pragma pack ()
Total member Size =9

2) Overall alignment
Overall alignment factor = min ((max (Int,short,char), 8) = 4


5, 16-byte alignment (#pragma pack (16))
Output: sizeof (struct test_t) = 12
Analysis Process:
1) alignment of member data
#pragma pack (16)
struct TEST_T {
int A; /* Length 4 < 16 press 4 to align; start offset=0; storage location interval [0,3] */
Char b; /* Length 1 < 16 press 1 to align; start offset=4; storage location interval [4] */
Short C; /* Length 2 < 16 press 2 to align; start offset=6; storage location interval [6,7] */
Char D; /* Length 1 < 16 press 1 to align; start offset=; storage location interval [8] Finally, 3 slots are added to meet the total length of 4 (int is 4 bytes, 4<8) integer Times */
};
#pragma pack ()
Total member Size =9

2) Overall alignment
Overall alignment factor = min ((max (Int,short,char), 16) = 4

Written at the end:

If there is an array in the struct, alignment does not align the array as a whole, but instead aligns each element of the array one at a time.

Linux Memory Alignment

Related Article

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.