A summary of the problem of the class-seeking sizeof
First look at the following code:
1 classA2 {3 Public:4 A () {};5~A () {};6 Private:7 intA;8 Doubleb;9 };Ten One classB: PublicA A { - Public: - B () {}; the~B () {}; - Private: - intA; - Charb; + }; - +cout <<sizeof(A) <<sizeof(B) <<endl;
First of all, class sizeof follows the following points:
1. Class size is the sum of non-static member types, which means that static member data, such as statics, is not the statistical range of sizeof.
The following code runs the result: 1.
1 struct A 2 {3 Char C; 4 Static int A; 5 }; 6 sizeof (A) <<endl;
2. Normal member function is not used as sizeof statistical range, including tectonic destructor.
The following code runs the result: 4.
1 class A 2 {3public:4 Show (); 5 Private : 6 int A; 7 }; 8 cout <<sizeof(A) <<endl;
3, virtual function to maintain the virtual function table, the use of a pointer, four bytes.
The following code runs the result: 4.
1 class A 2 {3public:4 Virtual A () {}; 5 }; 6 cout<<sizeof(A) <<endl;
4. Class adheres to the byte alignment rules.
Among them, byte alignment is explained as follows:
1. The offset=0 place of the first data member, after which each member is stored from the size of the member or a multiple of its members.
As in a 32-bit system
Char is 1 bytes and can be stored in any location;
int is 4 bytes, only 0-3 4-7 8-11 12-15 ... these positions begin to be stored;
Double is 8 bytes, only in 0-7 8-15 ... these positions begin to be stored;
Why do this: because you can increase the speed of the computer to read data, only from the multiple index of the data to find, do not have to find one or the jump.
2. When a data member is a struct or other class, it is stored at multiples of its largest child member.
1 structA2 {3 DoubleA;4 };5 6 structB7 {8 intA;9 A B;Ten }; Onecout <<sizeof(B) <<endl;
As in the above code, the sizeof of a is 8 bytes, first int A in B is [0,3], then a B is 8 bytes, the largest child member in AB is a double 8 bytes, so it must start from [8-15], so the answer is 16.
3. The final length is a multiple of its maximum member length.
1 struct A 2 {3 int A; 4 Char C; 5 }; 6 sizeof (A) <<endl;
The answer is: 8. The maximum member length of a is 4,int A[0-3],char c[4],[5-7] complement.
And listen to me step by step to explain:
Line Number 1: Declare Class A, if a space-time class, at this time sizeof (a) is 1, not 0, explanation can view my previous article "C + + empty class sizeof".
Line number 2-5: declared member function, not included in sizeof statistical range.
Line number 7-8: member variable int is 4 bytes, double is 8 bytes, then total is 12 bytes? No, according to byte alignment, the total size of Class A is 16 bytes.
In the same vein, class A is inherited from Class B, first with 16 bytes, the member variables int A and char B, the bytes 4 and 1, and the total size of 24 bytes According to the byte alignment.
The summary is more messy, welcome to discuss.
The sizeof size of the c+ class and the byte alignment of the class