One, what is aligned, 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 at any address, but the reality is that access to a particular variable is often at a specific memory address, which requires that all types of data be arranged in space according to certain rules, Instead of sequencing one after another, that's the alignment.
2. The role of alignment and reasons: each hardware platform for storage space processing is very different. Some platforms have access to certain types of data only from certain addresses. Other platforms may not be able to do this, but the most common is the loss of access efficiency if the data is not aligned according to the requirements for its platform. For example, some platforms begin every time they are read from my even address, if an int (assumed to be 32 bits) is stored at the beginning of the even address, then a read cycle can be read, and if stored at the beginning of the odd address, it may take 2 reading cycles, and two read the results of the high and low bytes 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
Usually, when we write a program, we don't need to consider the alignment problem. The compiler chooses the alignment policy 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 don't generally need to care about this problem, because the editor is aligned with data storage, and we don't understand it, it's often confusing to some questions. The most common is the struct data structure of the sizeof results, unexpected. To do this, we need to understand the alignment algorithm.
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 body is defined as follows:
struct A {
int a;
char b;
short c;
};
The structure body A contains a 4-byte length int, a 1-byte length char and a 2-byte length of the short data one. So the space used by a is supposed to be 7 bytes. However, because the compiler wants to align data members in space.
So using the sizeof (Strcut A) value is 8.
Now adjust the order of the member variables to the structure.
struct B {
char b;
int a;
short c;
};This is also a total of 7 bytes, but the value of sizeof (struct B) is 12.
Here we use the precompiled Instruction #pragma pack (value) to tell the compiler to replace the default by using our specified alignment value.
#pragma pack (2) /*指定按2字节对齐*/
struct C {
char b;
int a;
short c;
};
#pragma pack () /*取消指定对齐,恢复缺省对齐*/
sizeof(struct C)值是8。
The modified alignment value is 1:
#pragma pack (1) /*指定按1字节对齐*/
struct D {
char b;
int a;
short c;
};
#pragma pack () /*取消指定对齐,恢复缺省对齐*/
sizeof(struct D)值为7。