C ++ Class Object Memory Structure

Source: Internet
Author: User
C ++ Class Object Memory Structure

First, we will introduce the memory layout of class objects with inheritance relationships in C ++:
In C ++, if the class has a virtual function, it will have a virtual function table pointer _ vfptr, in the class object's initial memory data. It is followed by the memory data of the member variables in the class.
For child classes, the initial memory data records copies of parent class objects (including parent class virtual function table pointers and member variables ). The member variable data of the Child class.
The same principle applies to subclass. However, no matter how many sub-classes are inherited, there is always only one virtual function table pointer in the object.
 
 
 
To explore the memory layout of C ++ class objects, first write several classes and functions
First, write a base class:
Class base
{
Public:
Virtual void F () {cout <"base: F" <Endl ;}
Virtual void g () {cout <"base: G" <Endl ;}
Virtual void H () {cout <"base: H" <Endl ;}
Int base;
Protected:
PRIVATE:
};
Then, we study the structure of the sub-class Memory Object Based on Different inheritance conditions.
1. No virtual function set inheritance
 
// Subclass 1, without overload of virtual functions
Class Child1: public Base
{
Public:
Virtual void F1 () {cout <"Child1: F1" <Endl ;}
Virtual void G1 () {cout <"Child1: G1" <Endl ;}
Virtual void H1 () {cout <"Child1: h1" <Endl ;}
Int Child1;
Protected:
PRIVATE:
};
This subclass Child1 does not inherit any virtual function of the base class, so its virtual function table is as follows:
 
 
We can see that in the virtual function table of the subclass, the virtual functions of the base class are first stored, and the virtual functions of the subclass are stored.
 
2. There is a virtual function inherited
// Subclass 2, with one virtual function overload
Class child2: public Base
{
Public:
Virtual void F () {cout <"child2: F" <Endl ;}
Virtual void G2 () {cout <"child2: G2" <Endl ;}
Virtual void H2 () {cout <"child2: H2" <Endl ;}
Int child2;
Protected:
PRIVATE:
};
 
When the subclass reloads the virtual function of the parent class, the compiler replaces the virtual function of the parent class in the virtual function table of the Child class with the function of the Child class.
3. All virtual functions are inherited.
// Subclass 3, all virtual functions are overloaded
Class Child3: public Base
{
Public:
Virtual void F () {cout <"Child3: F" <Endl ;}
Virtual void g () {cout <"Child3: G" <Endl ;}
Virtual void H () {cout <"Child3: H" <Endl ;}
Protected:
Int X;
PRIVATE:
};
 
 
 
4. Multi-Inheritance
Multi-inheritance: A class has multiple parent classes. In this case, the memory structure of the Child classes is different from that of a single inheritance class.
 
We can see that when the subclass inherits multiple parent classes, the memory structure of the subclass is as follows:
Sub-class memory, order
 
5. Diamond inheritance
 
 
6. Single virtual inheritance
 
 
The memory structure of the subclass of virtual inheritance is completely different from that of normal inheritance. The virtual inherited subclass has a separate virtual function table. In addition, a virtual function table of the parent class is also saved separately. The two parts use a four-byte 0x00000000 as the boundary. In the sub-class memory, the first is its own virtual function table, then the data member of the sub-class, then 0x0, and then the virtual function table of the parent class, is a data member of the parent class.
If the child class does not have its own virtual function, the Child class does not have a virtual function table, but the child class data and the parent class data still need to be separated by 0x0.
Therefore, in virtual inheritance, data of child classes and parent classes are completely separated. First, the virtual function tables and data of child classes are stored, and the data is divided by 0x in the middle, finally, save the virtual functions and data of the parent class. If the subclass reloads the virtual function of the parent class, replace the corresponding function in the parent class virtual function table in the subclass memory.
 
7. Diamond virtual inheritance
 
Conclusion:
(1) For the base class, if there is a virtual function, first store the virtual function table pointer, and then store its own data members; if there is no virtual function, then directly store the data members.
(2) for a single inherited Class Object, first store the data copy of the parent class (including the virtual function table pointer) and then the data of this class.
(3) In the virtual function table, first store the virtual function of the parent class, and then store the virtual function of the subclass.
(4) If some virtual functions of the parent class are overloaded, the new virtual functions overwrite the virtual functions of the parent class in the virtual function table.
(5) For multi-inheritance, first store the data copy of the first parent class, and then store the data member of the second parent class. Each copy of the parent class contains a virtual function table pointer. If the subclass reloads a virtual function of a parent class, it overwrites the function of the parent class virtual function table. In addition, the virtual function of the subclass is stored in the part behind the virtual function table of the first parent class.
(6) When the virtual function of an object is called, the compiler queries the virtual function table of the object, finds the function, and then calls it.

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.