Memory alignment issues in C language

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, memory alignment reason most of the references are said: 1, platform reason (transplant reason): Not all hardware platform can access arbitrary data at any address, some hardware platform can only fetch certain types of data at some addresses, or throw a hardware exception. 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 each compiler on a particular platform has its own default "alignment factor" (also called Zimo number). 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), the first data member 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, The relatively small one is carried out. 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, test us through a series of examples of detailed instructions to prove this rule! I experimented with the compiler including the GCC 3.4.2 and VC6.0 C compilers, the platform for Windows XP + SP2.

We will use a typical struct to explain.  First we define a struct: #pragma pack (n)/* n = 1, 2, 4, 8, +/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 [two compiler output CONSISTENT] analysis process: 1) member Data Alignment #pragma pack (1) struct test_t {int  A  /* Length 4 < 1 press 1 to align, start offset=0 0%1=0; storage location interval [0,3] */char B; /* Length 1 = 1 press 1 to align; start offset=4 4%1=0; storage location interval [4] */short C;  /* Length 2 > 1 press 1 to align, start offset=5 5%1=0; storage location interval [5,6] */char D; /* Length 1 = 1 press 1 to align; start offset=7 7%1=0; storage location interval [7] */}; Total size of #pragma pack () member =8

2) Overall Alignment factor = min ((max (Int,short,char), 1) = 1 overall Size (size) =$ (total member size) rounded = 8/* 8%1=0 */[Note 1]

2, 2-byte alignment (#pragma pack (2)) output: sizeof (struct test_t) = 10 [two compiler output CONSISTENT] analysis process: 1) member Data Alignment #pragma pack (2) struct test_t {int  A  /* Length 4 > 2 press 2 to align, start offset=0 0%2=0; storage location interval [0,3] */char B; /* Length 1 < 2 press 1 to align, start offset=4 4%1=0; storage location interval [4] */short C;  /* Length 2 = 2 press 2 to align; start offset=6 6%2=0; storage location interval [6,7] */char D; /* Length 1 < 2 press 1 to align, start offset=8 8%1=0; storage location interval [8] */}; Total size of #pragma pack () member =9

2) Overall Alignment factor = min ((max (Int,short,char), 2) = 2 overall Size (size) =$ (total member size) rounded = 10/* 10%2=0 */

3, 4-byte alignment (#pragma pack (4)) output: sizeof (struct test_t) = 12 [two compiler output CONSISTENT] analysis process: 1) member Data Alignment #pragma pack (4) struct test_t {int  A  /* Length 4 = 4 press 4 to align; start offset=0 0%4=0; storage location interval [0,3] */char B; /* Length 1 < 4 press 1 to align, start offset=4 4%1=0; storage location interval [4] */short C;  /* Length 2 < 4 press 2 to align, start offset=6 6%2=0; storage location interval [6,7] */char D; /* Length 1 < 4 press 1 to align, start offset=8 8%1=0; storage location interval [8] */}; Total size of #pragma pack () member =9

2) Overall Alignment factor = min ((max (Int,short,char), 4) = 4 overall Size (size) =$ (total member size) rounded = 12/* 12%4=0 */

4, 8-byte alignment (#pragma pack (8)) output: sizeof (struct test_t) = 12 [two compiler output CONSISTENT] analysis process: 1) member Data Alignment #pragma pack (8) struct test_t {int  A  /* Length 4 < 8 press 4 to align, start offset=0 0%4=0; storage location interval [0,3] */char B; /* Length 1 < 8 press 1 to align, start offset=4 4%1=0; storage location interval [4] */short C;  /* Length 2 < 8 press 2 to align, start offset=6 6%2=0; storage location interval [6,7] */char D; /* Length 1 < 8 press 1 to align, start offset=8 8%1=0; storage location interval [8] */}; Total size of #pragma pack () member =9

2) Overall Alignment factor = min ((max (Int,short,char), 8) = 4 overall Size (size) =$ (total member size) rounded = 12/* 12%4=0 */

5, 16-byte alignment (#pragma pack (16)) Output: sizeof (struct test_t) = 12 [two compiler output CONSISTENT] analysis process: 1) member Data Alignment #pragma pack () struct test_t {  int A;  /* Length 4 < 16 press 4 to align, start offset=0 0%4=0; storage location interval [0,3] */char B; /* Length 1 < 16 press 1 to align, start offset=4 4%1=0; storage location interval [4] */short C;  /* Length 2 < 16 press 2 to align, start offset=6 6%2=0; storage location interval [6,7] */char D; /* Length 1 < 16 press 1 to align, start offset=8 8%1=0; storage location interval [8] */}; Total size of #pragma pack () member =9

2) Overall Alignment factor = min ((max (Int,short,char), 16) = 4 overall Size (size) =$ (total member size) rounded = 12/* 12%4=0 */

Iv. conclusion the 8-byte and 16-byte alignment tests prove the 3rd of the "rule": "when the n value of the #pragma pack equals or exceeds the length of all data members, the size of the n value will not produce any effect". In addition, the memory alignment is a very complex thing, the above mentioned in some cases may also be incorrect. Hehe ^_^

[Note 1] What is "rounding"? Example: As the above 8-byte alignment in the "overall alignment", the overall size =9 4 round = 12 round the process: starting from 9 each add one, see if it can be divisible by 4, where the 9,10,11 can not be divisible by 4, until 12 o'clock, the round end.

Memory alignment issues in C language

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.