Snap to memory

Source: Internet
Author: User


in recent projects, we have covered "Memory alignment" technology. For most programmers, "memory alignment" should be "transparent" to them. "Snap to memory" should be the "jurisdiction" of the compiler. The compiler arranges for each "data unit" in the program in the appropriate place. But one of the features of the C language is that it's too flexible and too powerful to allow you to intervene in "memory alignment." If you want to know more about the underlying secrets, "memory alignment" should not be transparent to you.

First, the reason for the memory alignment
Most of the references are as follows:
1. Platform reason (transplant reason): Not all hardware platforms can access arbitrary data on any address, and some hardware platforms can only take certain types of data at certain addresses, or throw hardware exceptions.
2, performance reasons: Data structure (especially the stack) should be as far as possible in the natural boundary alignment. The reason is that in order to access the misaligned memory, the processor requires two memory access, while the aligned memory access requires only one access.

Ii. Alignment Rules
Compilers on each particular platform have their own default "alignment coefficients" (also known as Zimo numbers). Programmers can change this coefficient by precompiling the command #pragma pack (n), where n is the "alignment factor" you want to specify.

Rules:
1. Data member Alignment rules: Structure (struct) (or union), the first data member is placed at offset 0, and each subsequent alignment of the data member is performed in the smaller of the value specified by the #pragma pack and the length of the data member itself.
2, the overall alignment rule of a struct (or union): After the data member has been aligned, the structure (or union) itself is aligned, and alignment is performed in the smaller of the value and structure (or union) of the maximum data member length specified by the #pragma pack.
3, combined with 1, 2 inferences: When the n value of the #pragma pack equals or exceeds the length of all data members, the size of this n value will not produce any effect.

Third, the test
Let's prove this rule through a series of examples of detailed instructions.
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 identify 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 trial 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 result: sizeof (struct test_t) = 8
Analysis Process:
1) member Data alignment
#pragma packs (1)
struct TEST_T {
int A; /* Length 4 < 1 by 1 alignment; starting offset=0; storage location interval [0,3] * *
Char b; /* Length 1 = 1 by 1 alignment; starting offset=4; storage location interval [4] * *
Short C; /* Length 2 > 1 by 1 alignment; starting offset=5; storage location interval [5,6] * *
Char D; /* Length 1 = 1 by 1 alignment; starting offset=7; storage location interval [7] * *
};
#pragma pack ()
Total member Size =8

2) The whole alignment
Overall alignment coefficient = min ((max (Int,short,char), 1) = 1

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

2) The whole alignment
Overall alignment coefficient = min ((max (Int,short,char), 2) = 2

3, 4-byte alignment (#pragma pack (4))
Output result: sizeof (struct test_t) = 12
Analysis Process:
1) member Data alignment
#pragma packs (4)
struct TEST_T {
int A; /* Length 4 = 4 by 4 alignment; starting offset=0; storage location interval [0,3] * *
Char b; /* Length 1 < 4 by 1 alignment; starting offset=4; storage location interval [4] * *
Short C; /* Length 2 < 4 by 2 alignment; starting offset=6; storage location interval [6,7] * *
Char D; /* Length 1 < 4 is aligned to 1, starting offset=8, storing position interval [8], and finally filling up 3 vacancies to meet the integer multiple of the total length of 4 (int is 4 bytes, 4=4) * *
};
#pragma pack ()
Total member Size =9

2) The whole alignment
Overall alignment coefficient = min ((max (Int,short,char), 4) = 4

4, 8-byte alignment (#pragma pack (8))
Output result: sizeof (struct test_t) = 12
Analysis Process:
1) member Data alignment
#pragma packs (8)
struct TEST_T {
int A; /* Length 4 < 8 by 4 alignment; starting offset=0; storage location interval [0,3] * *
Char b; /* Length 1 < 8 by 1 alignment; starting offset=4; storage location interval [4] * *
Short C; /* Length 2 < 8 by 2 alignment; starting offset=6; storage location interval [6,7] * *
Char D; /* Length 1 < 8 by 1 alignment; starting offset=8; storage location interval [8] Finally, a total of 3 vacancies to meet the integer multiple of 4 (int is 4 bytes, 4<8).
};
#pragma pack ()
Total member Size =9

2) The whole alignment
Overall alignment coefficient = min ((max (Int,short,char), 8) = 4


5, 16-byte alignment (#pragma pack (16))
Output result: sizeof (struct test_t) = 12
Analysis Process:
1) member Data alignment
#pragma packs (16)
struct TEST_T {
int A; /* Length 4 < 16 by 4 alignment; starting offset=0; storage location interval [0,3] * *
Char b; /* Length 1 < 16 by 1 alignment; starting offset=4; storage location interval [4] * *
Short C; /* Length 2 < 16 by 2 alignment; starting offset=6; storage location interval [6,7] * *
Char D; /* Length 1 < 16 by 1 alignment; starting offset=; storage location interval [8] Finally, a total of 3 vacancies to meet the integer multiple of 4 (int is 4 bytes, 4<8).
};
#pragma pack ()
Total member Size =9

2) The whole alignment
Overall alignment coefficient = min ((max (Int,short,char), 16) = 4

written at the end:

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

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.