C + + class memory layout diagram (member functions and member variables discussed separately)

Source: Internet
Author: User

First, member functions

The member function can be considered as a global function of the class scope, not in the space allocated by the object, only the virtual function will have a pointer in the class object, storing the address of the virtual function and other related information.

The address of the member function, the compile time is determined, and is statically bound or dynamically bound on the corresponding object.
When an object calls a member function, the compiler can determine the address of these functions as long as it is compiled, and by passing in the this pointer and other parameters, the function is called, so there is no need to store the information for the member function in the class.

Second, member variables

Transferred from: http://www.cnblogs.com/jerry19880126/p/3616999.html

Books on the inheritance of the relevant chapters to the end of this, here is the C + + memory distribution structure, let us see how the compiler handles the memory distribution of the class members, especially in the case of inheritance, virtual functions exist.

工欲善其事, its prerequisite, let's start with the Visual Studio Tools, like this step-by-step:

Select the c/c++-> command line on the left and then write the/d1 reportallclasslayout on the other option, which can see the memory layout of all related classes, if written on/d1 Reportsingleclasslayoutxxx (XXX is the class name), only the memory layout of the specified class XXX will be typed. This configuration is supported in recent VS versions.

Below you can define a class, like this:

1 class Base2 {3     int a;4     int b;5 public:6     void Commonfunction (); 7};

Then compile, and you can see that the output box has this arrangement:

You don't want to spend your energy on memory alignment factors, so member variables are set to int.

From here you can see the arrangement of the normal class, the member variables are arranged according to the Order of the Declaration (within the class offset is 0), the member function does not occupy memory space.

Take a look at the inheritance and add the following code to the back side:

1 class derivedclass:public Base2 {3     int c;4 public:5     void Derivedcommonfunction (); 6};

Compile, and then see the following memory distribution (the memory distribution of the parent class is the same, only the memory distributions of the subclass member variables are discussed here):

You can see that the subclass inherits the member variables of the parent class, and in the memory arrangement, the member variables of the parent class are arranged, and then the member variables of the subclass are arranged, and the member functions do not take up the bytes.

Let's add a virtual function to the base class, temporarily comment out the derivedclass, and look at the memory arrangement:

1 class Base2 {3     int a;4     int b;5 public:6     void Commonfunction () 7     void Virtual virtualfunction (); 8};

This memory structure is divided into two parts, the memory distribution, the following is the virtual table, we look at one by one. VS has the compiler to place the virtual table pointer at the beginning of the memory (0 address offset), and then the member variable, the following generated a virtual table, immediately after the &base1_meta of the 0, the virtual table corresponding to the virtual pointer in memory distribution, the following list of virtual functions, The left 0 is the ordinal of this virtual function, there is only one virtual function, so there is only one, if there are more than one virtual function, there will be a sequence number 1, the virtual function of 2 is listed.

The compiler creates this virtual table pointer and the virtual table in the constructor function.

So how does the compiler use virtual table pointers and virtual tables to achieve polymorphism? So, when you create an object that contains a parent of a virtual function, the compiler points the virtual table pointer to the virtual function of the parent class when the object is constructed, and similarly, when the object of the subclass is created, the compiler will set the virtual table pointer in the constructor (the subclass has only one virtual table pointer, It comes from the parent class) that points to the virtual table of the subclass (the virtual function entry address inside the virtual table is a subclass).

So, if it is called base *p = new Derived (), the object of the child class is generated, when constructed, the virtual pointer of the subclass object points to the virtual table of the subclass, and then the conversion from derived* to base* does not alter the virtual table pointer, so this time p-> Virtualfunction, in fact, is p->vfptr->virtualfunction, it is in the construction of the time has pointed to the subclass of Virtualfunction, so called the virtual function of the subclass, this is polymorphic.

Add the subclass below and the virtual function in the subclass, like this:

1 class derivedclass:public Base2 {3     int c;4 public:5     void Derivedcommonfunction (); 6     void virtual Virtualfunction (); 7};

You can see that the sub-class memory is arranged as follows:

The upper half is the memory distribution, you can see that the virtual table pointer is inherited, and still at the beginning of the memory arrangement, the following is the parent class member variables A and B, and finally the subclass of the member variable C, note that the virtual table pointer only one, the subclass does not generate a virtual table pointer, the second half of the virtual table is the same

Let's change the code for the class, like this:

1 class derivedclass1:public Base2 {3     int c;4 public:5     void Derivedcommonfunction (); 6     void Virtual virtualf Unction2 (); 7};

Notice that we did not overwrite the virtual method of the parent class, but instead declared a new subclass virtual method with the following memory distribution:

There is still only one virtual table pointer, but the contents of the virtual table below change, the No. 0 number of the virtual table is the virtualfunction of the parent class, and 1th is the VirtualFunction2 of the subclass. That is, if the DerivedClass object is defined, then at the time of construction, the virtual table pointer points to the virtual table, and later if the call is Virtualfunction, then the corresponding virtual function is sought from the parent class, if VirtualFunction2 is called. The corresponding virtual function is then looked up from the subclass.

We re-transform the class like this:

1 class derivedclass1:public Base2 {3     int c;4 public:5     void Derivedcommonfunction (); 6     void Virtual virtualf Unction (); 7     void Virtual VirtualFunction2 (); 8};

We overwrite both the virtual function of the parent class and the newly added virtual function, so we can expect that this memory distribution is the following:

The following is a discussion of multiple inheritance with the following code:

1 class Base 2 {3     int A; 4     int b; 5 public:6     void Commonfunction (); 7     void Virtual virtualfunction (); 8 }; 9 class Derivedclass1:public Base12 {     int c;14 public:15     void Derivedcommonfunction ();     void Virtua L virtualfunction ();};18 class Derivedclass2:public Base20 {+     int d;22 public:23     void Derivedcommonfuncti On ();     virtualfunction ();};26 class Derivedderivedclass:public DerivedClass1, public Derivedcla ss228 {     virtualfunction int e;30 public:31     void Derivedderivedcommonfunction ();     33 };

The memory distribution is from the parent class to the child class, in order as follows:

There is a virtual table pointer in base with an address offset of 0

DerivedClass1 inherits the base, and the memory arrangement is the post-class subclass of the father.

The situation of DERIVEDCLASS2 is similar to that of DerivedClass1.

Here we focus on this class Derivedderivedclass, from the outside, it is arranged in parallel to inherit the two parent class DerivedClass1 and DerivedClass2, as well as its own member variable E. DERIVEDCLASS1 contains its member variable C, and Base,base has a virtual table pointer with a 0 address offset, then a memory arrangement of member variables A and b;derivedclass2 similar to DerivedClass1, Notice that there is also a base in DerivedClass2.

Here are two copies of the table, respectively, for DerivedClass1 and DerivedClass2, the number below &deriveddericedclass_meta is the first address offset, The 16 of the virtual table below represents the memory offset of the virtual pointer to the virtual table, which is the memory offset of the {vfptr} in DerivedClass2 in Derivedderivedclass.

If you use virtual inheritance, like this:

1 class Derivedclass1:virtual Public Base 2 {3     int c; 4 public:5     void Derivedcommonfunction (); 6     void Virtu Al virtualfunction (); 7}; 8  9 Class Derivedclass2:virtual public Base10 {one     int d;12 public:13     void Derivedcommonfunction ();     void virtual virtualfunction ();};16 class Derivedderivedclass: Public  DerivedClass1, public DerivedClass218 {1 9     int e;20 public:21     void Derivedderivedcommonfunction ();     void virtual virtualfunction (); 23};

The base class does not change, but looks down:

DerivedClass1 has changed, the original is the first row of virtual table pointers and base member variables, VFPTR at 0 address offset; But now there are two virtual table pointers, one is vbptr, the other is vfptr. Vbptr is the virtual table pointer for this DerivedClass1, which points to DerivedClass1 's virtual table vbtable, and the other vfptr is a virtual pointer to the virtual base class table, which points to vftable.

Two virtual tables are listed below, the first table is the table pointed to by Vbptr, 8 represents the offset of {vbptr} and {vfptr}, the second table is the table that the vfptr points to, and 8 indicates the offset of the virtual pointer that corresponds to the memory in this table.

The memory distribution of the DERIVEDCLASS2 is similar to DerivedClass1, with two virtual pointers pointing to two virtual tables (the second is the virtual base class table).

Here is a closer look at the memory distribution of Derivedderivedclass, which has three virtual pointers, but only one copy of base. The first virtual table is DerivedClass1, 20 indicates its virtual pointer {vbptr} distance from the virtual base table pointer {vfptr}, the second virtual table contains DerivedClass2, 12 indicates its virtual pointer {vbptr} away from the virtual base table pointer {vfptr} Distance, the last table is a virtual base table, and 20 indicates the offset of its corresponding virtual pointer {vfptr} in memory.

The effect of virtual inheritance is to reduce the duplication of the base class, at the cost of increasing the burden of virtual table pointers (more virtual table pointers).

Here's a summary (when the base class has a virtual function):

1. Each class has virtual pointers and virtual tables;

2. If it is not a virtual inheritance, then the subclass inherits the virtual pointer of the parent class and points to its own virtual table, which occurs when the object is constructed. How many virtual functions are there, and how many items are in the virtual table. Multiple inheritance, there may be more than one base class virtual table and virtual pointer;

3. If the virtual inheritance, then the subclass will have two virtual pointers, one point to their own virtual table, the other point to the virtual base table, multiple inheritance when the virtual base table and virtual base table pointers have only one copy.

C + + class memory layout diagram (member functions and member variables discussed separately)

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.