C ++ memory alignment summary and memory alignment Summary
As we all know, the memory size of the C ++ class is 1 byte. To ensure that its objects have independent memory addresses. The non-empty class size is related to the non-static member variables of the class and the number of virtual function tables.
It is worth noting that the size of non-static member variables in the class is related to the settings of the compiler memory alignment.
The memory storage of member variables in the class is not necessarily continuous. It is stored according to the compiler settings and memory blocks. The value of the memory block size is memory alignment.
I. Introduction issues.
#include<iostream>using namespace std;class test {private : char c='1';//1byte int i;//4byte short s=2;//2byte};int main(){ cout << sizeof(test) << endl; return 0;}
Output: 12
class test2 {private: int i;//4byte char c = '1';//1byte short s = 2;//2byte};int main(){ cout << sizeof(test2) << endl; return 0;}
Output: 8
We can see that. The member variables of the test class and test2 are identical, but the definition order is different, but the size of memory occupied by the two classes is different. This is why the compiler memory alignment.
Ii. Rules
1. Place the first data member in the position where the offset is 0. In the future, the alignment of each data member will be carried out according to the value specified by # pragma pack and the length of the data member itself, which is relatively small.
2. After data members are aligned, the class (structure or union) itself also needs to be aligned. The alignment will follow the value and structure (or union) specified by # pragma pack) the smaller part of the maximum data member length.
Obviously # pragma pack (n) is used as a pre-compiled command to set how many bytes are aligned. It is worth noting that the default value of n is set by the compiler, which is generally 8. Valid values are 1, 2, 4, 8, and 16, respectively.
That is, the compiler only splits the memory according to the methods 1, 2, 4, 8, 16. If n is another value, it is invalid.
Iii. Problem Analysis
(1) the memory space of the test class is as follows:
Memory Allocation Process:
1. char is compared with the default memory split size of the compiler. char is relatively small and a byte is allocated to it.
2. Compare the default memory split size between int and the compiler. int is small, which occupies 4 bytes. Only 3 bytes can be blank and 4 bytes can be reallocated.
3. Compare short with the default memory split size of the compiler. short is relatively small and occupies 2 bytes. Allocate 2 bytes to it.
4. The alignment end class must also be aligned. Therefore, the remaining two bytes are also occupied by test.
(2) the memory space of test2 class is as follows:
1. Compare the default memory split size between int and the compiler. int is small, which occupies 4 bytes. Allocate four bytes to int.
2. The default memory split size of char is compared with that of the compiler. char is relatively small and a byte is allocated to it.
3. Compare short with the default memory split size of the compiler. short is relatively small. At this time, the remaining three bytes after the char allocation is completed, which is enough for short to store two bytes, so short is next to it. Allocate two bytes to short.
4. The alignment end class must also be aligned, so the last remaining 1 byte is also occupied by test.
(3) Use # pragma pack (n)
# Include <iostream> using namespace std; # pragma pack (1) // set to 1 byte alignment class test {private: char c = '1 '; // 1 byte int I; // 4 byte short s = 2; // 2 byte}; class test2 {private: int I; // 4 byte char c = '1'; // 1 byte short s = 2; // 2 byte}; int main () {cout <sizeof (test) <endl; cout <sizeof (test2) <endl; return 0 ;}
Output:
We can see that when we set the memory split size of the compiler to 1, all the member variables in the class are closely and continuously distributed.
Now, the C ++ memory alignment summary is almost done. To learn more about C ++ object memory allocation, we recommend Chen Hao's two articles:
Http://blog.csdn.net/haoel/article/details/3081328
And http://blog.csdn.net/haoel/article/details/1948051
Chen Hao wrote too thoroughly.
Reference: http://www.cppblog.com/snailcong/archive/2009/03/16/76705.html
Http://www.jb51.net/article/45406.htm