Calculation of memory consumption size of virtual function and (virtual) inheriting class in C + +

Source: Internet
Author: User

The memory size calculation of the class in the case of virtual inheritance

When there are multiple virtual functions in each base class, and in the case of virtual inheritance, how the memory is allocated and how the class size is computed, the following example illustrates:

#include <iostream>
using namespace Std;

Class A
{

Public
int A;
virtual void aa () {};
};

Class D
{
Public
virtual void dd () {};
};


Class C
{
Public
virtual void cc () {};
};

Class B:virtual public a,virtual public D, virtual public C
{

Public
int A;
virtual void bb () {};

};

sizeof (B) = 28,b class Although virtual inheritance of multiple base classes (A, D, c), but only a virtual base class pointer to the base class, but there are multiple virtual function table pointers, when class B does not have three base classes A, D, C of the virtual functions of the time, Their four classes have virtual function pointers pointing to their own virtual function tables. When class B overwrites the virtual function of base class A, memory changes, and sizeof (b) = 24. The virtual function Table of Class B modifies a. The new diagram for memory allocation is as follows:

Class A
{

Public
int A;
virtual void aa () {};
};

Class D
{
Public
virtual void dd () {};
};


Class C
{
Public
virtual void cc () {};
};

Class B:virtual public a,virtual public D, virtual public C
{

Public
int A;
virtual void aa () {};

};


Second, the memory size calculation of the class under the general inheritance case

When there are multiple virtual functions in each base class, and in the case of normal inheritance, how the memory is allocated and how the class is calculated, the following example illustrates:

#include <iostream>
using namespace Std;

Class A
{

Public
int A;
virtual void aa () {};
};

Class D
{
Public
virtual void dd () {};
};


Class C
{
Public
virtual void cc () {};
};

Class B:public A,public D, public C
{

Public
int A;
virtual void bb () {};
};


sizeof (B) = 20,b class Although virtual inheritance of multiple base classes (A, D, c), but only a virtual base class pointer to the base class, but there are multiple virtual function table pointers, when class B does not have three base classes A, D, C of the virtual functions of the time, The pointer to a virtual function of Class B is placed by default after the first inherited base class A. When class B overrides the virtual function of base class A, the virtual function pointer of class B overrides the function pointer of Class A override, at which time sizeof (b) = 20. The virtual function Table of Class B modifies a. The new diagram for memory allocation is as follows:

#include <iostream>
using namespace Std;

Class A
{

Public
int A;
virtual void aa () {};
};

Class D
{
Public
virtual void dd () {};
};


Class C
{
Public
virtual void cc () {};
};

Class B:public A,public D, public C
{

Public
int A;
virtual void aa () {};
};



In summary: The difference between virtual inheritance and ordinary inheritance is whether there is a virtual base class pointer vbptr that points to the base class address.

In the case of virtual inheritance, if the derived class does not overwrite the virtual function of the base class, it points to its virtual function table by its corresponding virtual function table pointer, but when it overwrites the virtual function of a base class, its virtual function address overwrites the virtual function table of the corresponding base class. No longer has its own independent virtual function table pointer.

In the case of ordinary inheritance, if the derived class does not overwrite the base class virtual function, its virtual function address is followed by default after the first inherited base class, if the function of a base class is overwritten, its virtual function address overwrites the corresponding base class's virtual function table. In the case of ordinary inheritance, a derived class does not have its own independent virtual function table pointer.


Here is a more complex example, after the use of virtual inheritance, and the virtual inheritance of a few classes for ordinary inheritance, note that at this time the ordinary inheritance is, there are each to execute their own base class of virtual base class pointers.

Class Commonbase
{
int co;
};

Class Base1:virtual Public Commonbase
{
Public
virtual void Print1 () {}
virtual void Print2 () {}
Private
int B1;
};

Class Base2:virtual Public Commonbase
{
Public
virtual void Dump1 () {}
virtual void Dump2 () {}
Private
int B2;
};

Class Base3:virtual Public Commonbase
{
Public
virtual void Dump1 () {}
virtual void Dump2 () {}
Private
int b3;
};

Class Derived:public Base1, public Base2, public Base3
{
Public
void Print2 () {}
void Dump2 () {}
Private
int D;
};

sizeof (Derived) = 44




View virtual function tables and class memory layouts under the VS2010 command line

When learning virtual functions under multiple inheritance, the virtual function table (vtable) of derived classes needs to be analyzed, but several hack vtable methods are found on the Internet, and the results are not satisfactory. Unexpectedly Ms Compiler (take VS2010 for example) has the compile option of printing vtable, it is very good!

1. Open "Visual Studio Command Prompt (2010)", as follows


The CMD has some tools, such as Cl.exe, that are compiled and linked under the VS2010 command line.


2. Use the/d1 reportallclasslayout or reportsingleclasslayoutxxx option of the CL command. The reportallclasslayout option here prints a large number of related classes of information, which is generally of little use. While the reportsingleclasslayoutxxx option of XXX represents the name of the class to be compiled (here XXX Class), print the memory layout of the XXX class and the virtual function table (if there is no corresponding class in the code, the option is invalid).

Examples are as follows

Cl/d1 reportSingleClassLayoutBase1 160.cpp


Calculation of memory consumption size of virtual function and (virtual) inheriting class in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.