In depth analysis of the size of classes in C++

  • 2020-04-01 21:40:10
  • OfStack

Let's start with an example:


#include <iostream>
 using namespace std;

 class A{};

 class B
 {
     int b;
     char c;
 };

 class C
 {
     int c1;    
     static int c2;
 };
 int C::c2 = 1;

 class D:public C,public B{
     int d;
 }; 
 int main()
 {
     cout<<"sizeof(A)="<<sizeof(A)<<endl;
     cout<<"sizeof(B)="<<sizeof(B)<<endl;
     cout<<"sizeof(C)="<<sizeof(C)<<endl;
     cout<<"sizeof(D)="<<sizeof(D)<<endl;

     return 0;
 }

The running result is:

Sizeof (A) = 1

Sizeof (B) = 8

Sizeof (C) = 4

Sizeof (D) = 16


For class A, although A is an empty class, but in order to carry out to instantiate the empty class, the compiler will often assigned to it A byte, so that A instantiation in memory after they have A unique address. For class B, B should be the size of the sizeof (int) + sizeof (char) = 5, but consider the memory alignment, B size should be 8 for class C, static member variables of A class to be placed in the global area, and not on A piece of ordinary members of the class. Static members of a class exist after the class is declared, while non-static members exist only after the class is instantiated. So the sizeof C is sizeof(int)=4. The size of D is the size of B+C + the size of its own data members, which is 16.

 

= = = = = = = = = = = = = = = = = = = = = = = = = = line here = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

The following is a discussion of the size of classes with virtual functions:


#include <iostream>
 using namespace std;

 class A
 {
 public:
     void virtual aa(){};
 };

 class B:public A
 {
     void virtual bb(){};
 };

 class C:virtual A
 {
 public:
     void virtual aa(){};
     void cc(){};
 };

 class D:virtual A
 {
 public:
     void virtual dd(){};
 };

 int main()
 {
     cout<<"sizeof(A)="<<sizeof(A)<<endl;
     cout<<"sizeof(B)="<<sizeof(B)<<endl;
     cout<<"sizeof(C)="<<sizeof(C)<<endl;
     cout<<"sizeof(D)="<<sizeof(D)<<endl;

     return 0;
 }

The running result is:

Sizeof (A) = 4

Sizeof (B) = 4

Sizeof (C) = 8

Sizeof (D) = 12

For class A, it contains A virtual functions, the compiler will as the virtual function to generate A virtual function table, to record the corresponding function address, therefore, in the class A memory address to A vfptr_A pointer to the virtual table, therefore the size of the class A pointer size, namely 4. (note that no matter how much A virtual function in the class, their size is 4, because the memory of you just need to save the pointer).

For class B, it is public inherited from A, although it also has A virtual function, but from the result, B and A should be in vtable (virtual table) of B, so the size of class B is 4.

For class C, it's vitual inherits A, so you have to have A pointer to the parent class A, which takes up 4 bytes, aa () is A virtual function inherited from class A, and as A result, it doesn't take up space in memory, so the sizeof C is sizeof(A)+4=8.

For class D, it's virtual inheritance class A, same as above, you have to have A pointer to the parent class A, and also, class D has A virtual function, so you have to have A pointer to the virtual table, so sizeof (D)=sizeof (A) +4+4=12


Related articles: