In the same alignment mode, the data in the struct is defined in different order, and the overall occupied memory space of the struct is also different, as shown below:
The struct is defined as follows:
Struct
{
Int;
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;
Short c;
};
At this time, it is also a total of 7 bytes of variables, but the value of sizeof (struct B) is 12.
The following uses the pre-compiled command # progma pack (value) to tell the compiler to replace the default value with the specified alignment value.
# Progma pack (2)/* specify to be aligned by 2 bytes, equivalent to # pragma pack (push, 2 )*/
Struct C
{
Char B;
Int;
Short c;
};
# Progma pack ()/* cancel the specified alignment and restore the default alignment. It is equivalent to # pragma pack (pop )*/
The value of sizeof (struct C) is 8.
Modify the alignment value to 1:
# Progma pack (1)/* specify to align by 1 byte */
Struct D
{
Char B;
Int;
Short c;
};
# Progma pack ()/* cancel the specified alignment and restore the default alignment */
The sizeof (struct D) value is 7.
For char data, its own alignment value is 1, for short 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: # The alignment value specified for progma pack (value.
3. The alignment value of a struct or class: The value with the largest alignment value among its data members.
4. Valid alignment values of data members, struct, and classes: the alignment value of the data itself and the value smaller than the specified alignment value.
With these values, we can easily discuss the data structure members and their alignment. The valid alignment value N is the final value used to determine the data storage address. Valid alignment means "alignment on N", that is, the "Starting address for storing the data % N = 0 ". data variables in the data structure are discharged in the defined order. 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 multiple of the valid alignment values of the struct, ). In this way, you cannot understand the values of the above examples.
Example Analysis:
Analysis example B;
Struct B
{
Char B;
Int;
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.
The first member variable B's own alignment value is 1, which is smaller than the specified or default alignment value 4. Therefore, the valid alignment value is 1, therefore, the 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 4, therefore, it can only be stored in the four consecutive bytes from the starting address 0x0004 to 0x0007, which conforms to 0 x 0004% 4 = 0 and is 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 byte space from 0x0008 to 0x0009, 0 x 0008% 2 = 0. Therefore, B content is stored from 0x0000 to 0x0009.
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 the structure, 0x0009 to 0x0000 = 10 bytes, (10 + 2) % 4 = 0. Therefore, 0x0000A to 0x000B is also occupied by struct B. Therefore, B has 12 bytes from 0x0000 to 0x000B, and sizeof (struct B) = 12;
-----------------------
Alignment (offset of the starting address of the variable to the starting address of the structure)
Char
The offset must be a multiple of sizeof (char), that is, 1.
Int
The offset must be a multiple of sizeof (int), that is, 4.
Float
The offset must be a multiple of sizeof (float), that is, 4.
Double
The offset must be a multiple of sizeof (double), that is, 8.
Short
The offset must be a multiple of sizeof (short), that is, 2.
When each member variable is stored, the space is requested in sequence based on the order in which the structure appears, and the positions are adjusted according to the alignment above. The vacant byte VC is automatically filled. At the same time, to ensure that the size of the structure is a multiple of the number of byte boundary values of the structure (that is, the number of bytes occupied by the Type occupying the maximum space in the structure, therefore, after applying for space for the last member variable, the vacant bytes will be automatically filled as needed.
The following uses the previous example to illustrate how VC stores the structure.
Struct MyStruct
{
Double dda1;
Char dda;
Int type
};
When allocating space for the above structure, VC allocates space for the first member dda1 according to the sequence and alignment of the member variables, the starting address is the same as the starting address of the structure (the offset 0 is just a multiple of sizeof (double). The member variable occupies eight bytes; next, allocate space for the second member dda. the offset of the next address that can be allocated to the starting address of the structure is 8, which is a multiple of sizeof (char, therefore, the dda is stored in an alignment where the offset is 8. This member variable occupies sizeof (char) = 1 byte, and then allocates space for the third member type, in this case, the offset of the next allocable address to the starting address of the structure is 9, not a multiple of sizeof (int) = 4. To meet the offset constraints of alignment, VC automatically fills in three bytes (these three bytes do not have anything). At this time, the next address that can be allocated has a 12 offset to the starting address of the structure, which is exactly sizeof (int) = a multiple of 4, so the type is stored in the offset Where the number is 12, the member variable occupies sizeof (int) = 4 bytes. At this time, the member variables in the entire structure have been allocated space, and the total occupied space is: 8 + 1 + 3 + 4 = 16, which is exactly the number of bytes in the structure (that is, the number of bytes occupied by the Type occupying the maximum space in the structure sizeof (double) = 8) so there is no vacant byte to fill. Therefore, the size of the entire structure is sizeof (MyStruct) = 8 + 1 + 3 + 4 = 16. Among them, three bytes are automatically filled by VC and nothing makes sense.
Next, let's take another example to change the position of the member variable of MyStruct above to the following:
Struct MyStruct
{
Char dda;
Double dda1;
Int type
};
How much space does this structure occupy? In the VC6.0 environment, we can obtain that sizeof (MyStruc) is 24. Based on the space allocation principles mentioned above, we will analyze how VC allocates space for the above structure. (Simple description)
Struct MyStruct
{
Char dda; // The offset is 0. The alignment is satisfied and the dda occupies 1 byte;
Double dda1; // the offset of the next available address is 1, not sizeof (double) = 8
// Multiple. You need to add 7 bytes to make the offset 8 (the alignment is satisfied ).
// Method). Therefore, VC automatically fills in 7 bytes, and dda1 is stored at an offset of 8
// Address, which occupies 8 bytes.
Int type; // the offset of the next available address is 16, which is twice that of sizeof (int) = 4.
// Number, which meets the alignment of int, so no VC auto-fill is required.
// Put the address at the offset of 16, which occupies 4 bytes.
}; // All member variables are allocated space. The total size of the space is 1 + 7 + 8 + 4 = 20, not a structure.
// Number of knots (that is, the number of bytes occupied by the largest space type in the structure sizeof
// (Double) = 8), so four bytes need to be filled to meet the structure size
// Sizeof (double) = a multiple of 8.
Therefore, the total size of this structure is: sizeof (MyStruc) is 1 + 7 + 8 + 4 + 4 = 24. Among them, 7 + 4 = 11 bytes are automatically filled by VC, and nothing makes sense.
The special processing of the structure storage by VC does increase the speed of the CPU storage variable, but sometimes it also brings some trouble. We also shield the default alignment of the variables, you can set the alignment of variables.
# Pragma pack (n) is provided in VC to set the variable to n-byte alignment. N-byte alignment means the offset of the Start address of the variable. First, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment mode, second, if n is less than the number of bytes occupied by the variable type, the offset is a multiple of n, and the default alignment is not required. The total size of the structure also has a constraint, which is divided into the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the space occupied by the largest variable;
Otherwise, it must be a multiple of n. The following is an example of its usage.
# Pragma pack (push) // save alignment status
# Pragma pack (4) // set to 4-byte alignment
Struct test
{
Char m1;
Double m4;
Int m3;
};
# Pragma pack (pop) // restore alignment
The size of the above structure is 16. Next we will analyze its storage situation. First, we will allocate space for m1, and its offset is 0, which meets our own alignment (4-byte alignment ), m1 occupies 1 byte. Then we start to allocate space for m4. At this time, the offset is 1 and three bytes need to be supplemented. In this way, the offset must be a multiple of n = 4 (because sizeof (double) is greater than n ), m4 occupies 8 bytes. Then allocate space for m3. At this time, the offset is 12, which must be a multiple of 4. m3 occupies 4 bytes. At this time, space has been allocated for all member variables. A total of 16 bytes are allocated, which is a multiple of n. If you change # pragma pack (4) to # pragma pack (16), the size of the structure is 24. (Please analyze it by yourself)