1. What is alignment and why:
1. in modern computers, memory space is divided by byte. Theoretically, it seems that access to any type of variables can start from any address, however, the actual situation is that access to specific variables is often performed at specific memory addresses, which requires various types of data to be arranged in space according to certain rules, instead of sequential emissions, this is alignment.
2.
Alignment functions and causes: the processing of storage space varies greatly by hardware platform. Some platforms can only access certain types of data from some specific addresses. This may not happen on other platforms,
However, if the data storage is not aligned according to the requirements of the appropriate platform, the access efficiency will be compromised. For example, some platforms start with an even address each time they read data.
32-bit) if it is stored at the beginning of the even address, one read cycle can be read. If it is stored at the beginning of the odd address, two read cycles may be required, and the result of two reads
To obtain the int data. Obviously, reading efficiency is greatly reduced. This is also a game of space and time.
Ii. Alignment implementation
Normally, we writeProgramYou do not need to consider alignment issues. The compiler selects an alignment policy suitable for the target platform for us. Of course, we can also notify the compiler to pass the pre-compilation command to change the Alignment Method for the specified data.
However, because we generally do not need to care about this issue, the editor is aligned with the data storage. If we do not know it, we are often confused about some problems. The most common result is the sizeof result of the struct data structure, which is unexpected. Therefore, we need to alignAlgorithmUnderstand.
Alignment Algorithm:
The GCC version 3.2.2 Compiler (32-bit x86 Platform) is used as an example, to discuss how the compiler alignment each member in the struct data structure.
The struct is defined as follows:
Struct a {int A; char B; short C ;};
Struct a contains a four-byte int, a one-byte char, and a two-byte short data. Therefore, the space used by a is 7 bytes. However, the compiler must align the data members in space.
Therefore, the sizeof (strcut a) value is 8.
Now adjust the sequence of the member variables for this struct.
Struct B {char B; int A; short C ;};
At this time, it is also a total of 7 bytes of variables, but the value of sizeof (struct B) is 12.
Next we will use the pre-compiled command # pragma pack (value) to tell the compiler to replace the default value with the specified alignment value.
# Progma pack (2)/* specify to align by 2 bytes */struct c {char B; int A; short C ;};# progma pack ()/* 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 to align by 1 byte */struct d {char B; int A; short C ;};# progma pack ()/* cancel the specified alignment, restore the default alignment */sizeof (struct D) to 7.
PairFor char-type data, its own alignment value is 1, for Short-type data is 2, for int, float, double type, its own alignment value is 4, in bytes.
There are four conceptual values:
1) alignment of the Data Type itself: Alignment of the basic data type described above.
2) Specify the alignment value: # specify the alignment value when Pragma pack (value) is used.
3) alignment value of a struct or class: The value with the largest alignment value among its members.
4) Valid alignment values of data members, struct, and classes: the alignment value itself and the smaller value in the specified alignment value.
With these values, we can easily discuss the data structure members and their alignment.Yes
The valid alignment value n is the final value used to determine the data storage address. Effective alignment means "alignment on N", that is, the "Storage Start address % n = 0" of the data, while the data structure
All data variables are discharged in the defined sequence. The starting address of the first data variable is
The starting address of the data structure. The member variables of the struct must be aligned and discharged, and the struct itself must be rounded according to its own valid alignment values (that is, the total length occupied by the member variables of the struct must be an integer of the valid alignment values of the struct ).
Several times, which can be understood in the following example ).In this way, it is not difficult to understand the values of the above examples.
Example Analysis:
Analysis example B;
Struct B {char B; int A; short C ;};
Assume that B is discharged from the address space 0x0000. The alignment value is not defined in this example. In the author's environment, this value is 4 by default. First
The alignment value of member variable B is 1.
The fixed alignment value is 4 small, so its valid alignment value is 1, so its storage address 0x0000 conforms to 0 x 0000% 1 = 0. the alignment value of the second member variable A is 4, so the valid alignment value is also
4. Therefore, it can only be stored in the four consecutive bytes from the starting address 0x0004 to 0x0007. Check that 0 x 0004% 4 = 0 is close to the first variable. The third variable C is self-aligned.
The value is 2, so the valid alignment value is also 2. It can be stored in the two bytes from 0x0008 to 0x0009, Which is 0 x 0008% 2 = 0. Therefore, from 0x0000 to 0x0009
All content is B. Then, let's look at the alignment value of Data Structure B as the maximum alignment value in its variable (here it is B), so it is 4, so the valid alignment value of the structure is also 4. According to the requirements of structure rounding,
0x0009 to 0x0000 = 10 bytes, (10 + 2) % 4 = 0. Therefore, 0x0000a to 0x000b is also occupied by struct B. Therefore, B ranges from 0x0000 to 0x000b.
A total of 12 bytes, sizeof (struct B) = 12;
Similarly, analyze the above example C:
# Pragma pack (2)/* specify to align by 2 bytes */struct c {char B; int A; short C ;};# Pragma pack ()/* cancel the specified alignment, restore Default alignment */
The first variable B's own alignment value is 1 and the specified alignment value is 2. Therefore, the valid alignment value is 1. Assume that C starts from 0x0000
B is stored in 0x0000 and conforms to 0 x 0000% 1 =
0; the second variable, its own alignment value is 4, and the specified alignment value is 2, so the valid alignment value is 2, therefore, the sequence is stored in four consecutive periods: 0x0002, 0x0003, 0x0004, and 0x0005.
In bytes, it must be 0 x 0002% 2 = 0. The alignment value of the third variable C is 2, so the valid alignment value is 2, which is stored in sequence.
In 0x0006, 0x0007
0 x 0006% 2 = 0. Therefore, from 0x0000 to 0x00007, a total of eight characters are stored in the C variable. And C's own alignment value is 4, so
The valid alignment value of C is 2. Again 8% 2 = 0, C only occupies eight bytes from 0x0000 to 0x0007. So sizeof (struct c) = 8.
Yes
I believe you have a clear understanding of the concept of byte alignment in C. In network programs, it is very important to master this concept.
If a binary stream (such as a struct) is transmitted between Linux and Linux, the two platforms must define the same alignment mode. Otherwise, some errors may occur, but it is difficult to troubleshoot