Data Alignment
About the data alignment problem, now more or less have some contact, simply say their views.
1, the alignment of the background
Big and small end problems need to be introduced here, each address cell in the computer corresponds to a byte, a byte of 8bit, for a processor with a bit greater than 8 bits, the register width is greater than one byte, such as the 16bit short variable x, the address in memory is 0x0010 , the value of x is 0x1122,0x11 high byte, 0x22 is low byte, common X86 structure is small end mode, many arm,dsp are small end mode, and Keil C51 is big-endian mode. The memory space is divided by Byte, in theory, access to any type of variable can start at any address, but actually accessing a particular variable often takes place in a particular memory address, which requires that all types of data be arranged in space, not sequentially, in a certain rule, which is alignment.
2, the reason for the alignment
The processing of storage space varies from one hardware platform to another, and some platforms can only access certain types of data from certain specific 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.
3, alignment of the implementation
Usually we do not need to consider the effect of alignment when writing code, relying on the compiler to select the appropriate alignment policy for us, or we can specify the data alignment by passing the compiler precompiled instruction.
Taking the sizeof method of struct data structure as an example, the environment is the MAC OS X 64-bit kernel, and the structure is defined as follows:
struct A {
int A;
Char b;
Short C;
};
struct B {
Char b;
int A;
Short C;
};
#pragma packs (2)
struct C {
Char b;
int A;
Short C;
};
#pragma pack ()
#pragma packs (1)
struct D {
Char b;
int A;
Short C;
};
#pragma pack ()
int main (int argc, char** argv)
{
printf ("Size of struct A:%lu \ n", sizeof (struct a));
printf ("Size of struct B:%lu \ n", sizeof (struct B));
printf ("Size of struct C:%lu \ n", sizeof (struct C));
printf ("Size of struct D:%lu \ n", sizeof (struct d));
return 0;
}
Output:
Size of struct A:8
Size of struct B:12
Size of struct C:8
Size of struct D:7
The structure body contains a 4-byte length int, a 1-byte length char and a 2-byte length of the short one. The amount of memory used to add up is 7 bytes, but the actual use of sizeof found that the memory occupied by the structure is different.
There are several points to note about alignment:
(1) The alignment value of the data type itself: the intrinsic alignment value of the base data type, and the char type is 1,short type 2,int,float,double to 4;
(2) Specify the alignment value: The specified alignment value value when #pragma pack (value);
(3) The value of the structure or the self alignment of a class: The value of the maximum value in its member's own alignment;
(4) Valid alignment values for data members, structs, and classes: Self aligned values or smaller values in specified alignment values.
For a specific data structure member and its own alignment, the effective alignment of the value n will ultimately determine the way the data is stored in the address of the value, alignment on N means that the data "stored starting address%n=0",
Here is an analysis of the above examples:
struct B {
Char b;
int A;
Short C;
};
Assuming B starts with the address space 0x0000, the default alignment value is 4 (here's a question to ask you, my 64-bit kernel, but test my default alignment is 4, the first member variable B's own alignment value is 1, smaller than the default value of the valid alignment value of 1, store address 0x0000%1= 0, the second member variable A, its own alignment value of 4, the starting address is 0x0004 to 0x0007 this 4 consecutive byte space, 0x0004%4=0, the third variable C, its own alignment value of 2, the starting address of the deposit is 0x0008 to 0x0009, the address also meet the requirements. The value of the structure body B's own alignment is the maximum alignment value (b) 4 in the variable (10+2)%4=0, so 0x000a to 0x000b is also occupied by struct B.
In-memory diagram:
B--
A A A A
C C
#pragma packs (2)
struct C {
Char b;
int A;
Short C;
};
#pragma pack ()
The first variable B has its own alignment value of 1, the specified alignment value is 2, and the valid alignment value is 1,b 0x0000,a's own alignment value is 4, greater than the specified alignment value, and all valid alignment values are 2,a bytes are 0x0002, 0x0003, 0x0004, 0x0005 four consecutive bytes, C has its own alignment value of 2, so the valid alignment value is also 2, sorted in 0x0006, 0x0007. Structure C has its own alignment value of 4, so the valid alignment value is 2,8%2=0.
In-memory diagram:
B-
A A
A A
C C
In fact, think of the schematic diagram in memory everything will be much simpler.