C Language: Memory byte alignment explained [reprint]

Source: Internet
Author: User

One, what is alignment, and why to align:

1. The memory space in modern computers is divided by byte, theoretically it seems that access to any type of variable can start from any address, but the reality is that when accessing a particular variable, it is often accessed at a specific memory address, which requires all types of data to be spatially arranged according to certain rules, Instead of sequentially one by one emissions, that's the alignment.

2. The role and cause of alignment: the processing of storage space varies greatly with each hardware platform. Some platforms can only access certain types of data from certain specific addresses. Other platforms may not, but the most common is the loss of access efficiency if data storage is not aligned as required for its platform. For example, some platforms each read from the even address, if an int (assuming 32-bit) if it is stored at the beginning of the even address, then a reading period can be read out, and if stored in the location of the odd address, it may require 2 read cycles, and two read the results of the high and low Byte is pieced together to get the int data. Obviously, the reading efficiency is much lower. This is also the game of space and time.

Second, the realization of alignment

Normally, when we write a program, we don't need to consider alignment issues. The compiler chooses the alignment strategy for the target platform for us. Of course, we can also notify the compiler to pass the precompiled instructions and change the alignment of the specified data.
However, because we generally do not need to care about this problem, because the editor to the data storage alignment, and we do not understand, often will be confused about some problems. The most common is the sizeof result of the struct data structure, unexpectedly. To do this, we need to understand the alignment algorithm.
The Alignment algorithm:
Because of the different platforms and compilers, I use the GCC version 3.2.2 compiler (32-bit x86 platform) as an example to discuss how the compiler aligns the members of the struct data structure.
The structure is defined as follows:
struct A {
int A;
Char b;
Short C;
};
Struct A contains a 4-byte-length int, a 1-byte length char, and a 2-byte length of short data. So the space used for a should be 7 bytes. But because the compiler wants to align the data members spatially.
So using the sizeof (Strcut A) value is 8.
Now adjust the order of the member variables to the struct body.
struct B {
Char b;
int A;
Short C;
};
This is also a total of 7 byte variables, but sizeof (struct B) has a value of 12.
Below we use the precompiled Instruction #pragma pack (value) to tell the compiler to replace the default with our specified alignment value.
#progma Pack (2)/* Specify 2-byte alignment */
struct C {
Char b;
int A;
Short C;
};
#progma Pack ()/* To cancel the specified alignment, restore the default alignment */
The sizeof (struct C) value is 8.

Modify the alignment value to 1:
#progma Pack (1)/* Specify 1-byte alignment */
struct D {
Char b;
int A;
Short C;
};
#progma Pack ()/* To cancel the specified alignment, restore the default alignment */
The sizeof (struct D) value is 7.

RightFor char type data, its own alignment value is 1, for the short type is 2, for the int,float,double type, its own alignment value is 4, the unit byte.
There are four concept values in this:
1) The alignment value of the data type itself: is the self-aligning value of the underlying data type that was confessed above.

2) Specifies the alignment value: The specified alignment value when the #pragma pack (value) is values.

3) The self-aligning value of a struct or class: The value that is the largest of its members in its own alignment value.

4) Valid alignment values for data members, structs, and classes: their own alignment values and the smaller values in the specified alignment values.

with these values, we can easily discuss the members of a specific data structure and its own alignment. The valid alignment value n is the value that is ultimately used to determine how the data is stored, most importantly. A valid alignment of n is the "alignment on n", which means that the data "holds the starting address%n=0". Data variables in the structure are emitted in a defined order of precedence. The starting address of the first data variable is the starting address of the structure. The member variables of the struct should be aligned with the emission, and the structure itself should be rounded according to its own valid alignment value (that is, the total length of the struct member variable is required to be an integral multiple of the effective alignment value of the struct, as the following example understands). This makes it easy to understand the values of several examples above.
Example Analysis:
analysis example B;
struct B {
Char b;
int A;
Short C;
};
Suppose B starts discharging from the address space 0x0000. In this example, the specified alignment value is not defined, and in the author's environment, the value defaults to 4. The first member variable B has a self-aligning value of 1, which is smaller than the specified or default alignment value of 4, so its valid alignment value is 1, so its storage address 0x0000 conforms to 0x0000%1=0. The second member variable A has its own alignment value of 4, so the valid alignment value is also 4, so it can only be stored in the starting address of 0x0004 to 0x0007 four contiguous byte space, review 0x0004%4=0, and immediately close to the first variable. The third variable, C, has its own alignment value of 2, so the valid alignment value is also 2, which can be stored in the two byte space of 0x0008 to 0x0009, conforming to 0x0008%2=0. So everything from 0x0000 to 0x0009 is stored in B content. Then look at the data structure B's own alignment value for its variable maximum alignment value (here is B) so is 4, so the structure of the effective alignment value is also 4. 0x0009 to 0x0000=10 bytes, (10+2)%4=0 according to the requirements of the structural rounding. So 0x0000a to 0x000b is also occupied by struct B. So B has a total of 12 bytes from 0x0000 to 0x000b, sizeof (struct B) = 12;

Similarly, analyze the above example C:
#pragma pack (2)/* Specify 2-byte alignment */
struct C {
Char b;
int A;
Short C;
};
#pragma pack ()/* To cancel the specified alignment, restore the default alignment */
The first variable B has its own alignment value of 1, the specified alignment value is 2, so its valid alignment value is 1, assuming that C starts with 0x0000, then B is stored in 0x0000, conforms to 0x0000%1= 0, the second variable has its own alignment value of 4, and the alignment value is 2, so the valid alignment value is 2. So the order is stored in 0x0002, 0x0003, 0x0004, 0x0005 four consecutive bytes, in accordance with 0x0002%2=0. The third variable, C, has its own alignment value of 2, so the valid alignment value is 2, which is stored sequentially
In 0x0006, 0x0007, in accordance with 0x0006%2=0. So from 0x0000 to 0x00007 a total of eight bytes is stored in the C variable. and C has its own alignment value of 4, so the valid alignment value for C is 2. And 8%2=0,c only takes up eight bytes of 0x0000 to 0x0007. So sizeof (struct C) =8.

With the above explanation, I believe you should have a clear understanding of the concept of byte alignment in C language. In the network program, mastering this concept is very important, oh, between the different platforms (such as between Windows and Linux) to pass 2 of streams (such as the structure), then in these two platforms must define the same alignment, or somehow out of some wrong, but it is difficult to troubleshoot the OH ^_^.

C Language: Memory byte alignment explained [reprint]

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.