Class Object Memory layout, virtual functions, virtual inheritance and multi-inheritance implementation

Source: Internet
Author: User

Class Object Memory layout, virtual functions, virtual inheritance and multi-inheritance Implementation Mechanism

1. classes without inheritance relationships

2. Single inheritance

2.1 single-layer inheritance

2.2 multi-Inheritance

3. Multi-Inheritance


1. classes without inheritance relationships

Class A and Class B are known. Class A represents A class without virtual functions, and Class B represents A class with virtual functions. The following section uses letters to indicate the class types mentioned above.

ClassA {public: int a_1; int a_2; A (int v1 = 1, int v2 = 2): a_1 (v1), a_2 (v2) {} void show () {cout <"class A" <endl ;}}; classB {public: int B _1; int B _2; B (int v1 = 1, int v2 = 2 ): B _1 (v1), B _2 (v2) {}void virtual show () {cout <"class B" <endl ;}}; perform the following tests: {// cout <"baseclass objects size:" <
 
  

The result is as follows:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + utzD98/Uo6xBwOC21M/ztcTE2rTmsry + bytes "1" cellspacing = "0" cellpadding = "0">

Int a_1

Int a_2

In B, the pointer pointing to the virtual function table (vptr) is first put. In this example, the storage address of the virtual function table is 0x4290756. The memory layout of Class B objects is as follows:

Vptr_ B

Int B _1

Int B _2

Ii. Single inheritance 1. Single-layer inheritance 1) subclass derived from Class:

ClassC: Non-incrementing virtual function, normal inheritance

ClassD: provides virtual functions, which are inherited

ClassG: Virtual inheritance without adding virtual functions

ClassH: provides virtual functions and virtual inheritance.

The UML class diagram is as follows:

The test code is as follows:

{// The size of the class Object cout <"Class objects 'size derived from class A:" <
   
    
Expected results:
    


We can see that the memory layout of class C objects is as follows:

Int a_1

Int a_2

Int c_1

Class D Objects: because of the introduction of virtual functions in Class D, a virtual function table pointing to the vptr will be stored at the beginning of the memory of the D object, and a virtual function address will be stored, the value is 0x04264351 (expressed as the D: display () Address of the virtual function with & D: display ).

The memory layout is as follows:

G Class Object: because it is a virtual inheritance, a vbptr will be stored at the beginning of g memory, pointing to the pointer to the virtual base table. The virtual function table stores two offset addresses: 0 and 8, respectively, 0 indicates the offset between the g object address and the address where the vptr pointer is stored, and 8 indicates the offset between the Object part of the g object and the address where the vbptr pointer is stored (& h () -& vbpt indicates, where & h (a) indicates the address of Part a in object h), and the shared part is placed after the unchanged part, these assignments are implicitly implemented by the compiler in the G constructor. The g memory layout is as follows:


H Class Object: a virtual function is added and inherited in virtual mode. Therefore, two pointers are allocated in the memory: vptr_h and vbptr_h, based on the program and display results, it is easy to judge that vptr_h is placed before vbptr_h. H memory layout is as follows:


2) subclass derived from Class B:

Class E: Non-incrementing virtual function, normal inheritance

ClassF: provides virtual functions.

ClassI: Virtual inheritance without adding virtual functions

ClassJ: provides virtual functions and virtual inheritance.

UML icons are the relationships between them as follows:

Follow the same test method above to obtain the result:


We can see that the memory layout of each object is as follows:

Class E object: virtual functions appear in the base class when it is not virtual inheritance, at the beginning of the memory layout of the derived class object, it points to the virtual function in the derived class (whether inherited or overwritten by the derived class ). However, for virtual inheritance, the situation is different, which can be observed below. E memory layout is as follows:


Class F object: when the base class has a virtual function, the derived class contains a new virtual function, and the Inheritance Method is single non-virtual inheritance, the derived class only has a virtual function table, refers to the virtual functions in the base class and derived class. In this example, f has the following memory layout:


Class I objects: When Class I objects are constructed using constructors, 1 byte of memory is left between the shared area and the unchanged area (0 is stored), so the memory occupied is 24 bytes, however, only 20 bytes are required. Similar situations also occur in the J class. After a simple test, Walkerczb finds that the base class has virtual functions. When a derived class using virtual inheritance is constructed using a constructor, 1 byte memory is reserved between the shared part and the unchanged part of the object memory of the derived class; however, if you use the direct value assignment method to construct a derived class object, no.

Use

I i;i.a_1=1;i.a_2=2;i.i_1=3

The I object constructed by this direct value assignment method acts on the f object using the same method. The test result is as follows:


The editor does not understand the implementation mechanism of redundant memory blocks when calling constructor. Therefore, an object is constructed using direct value assignment to describe the memory layout of I and j, the I memory layout is as follows (vbptr_ I _ B indicates a pointer to the virtual function table, which stores the Implementation of the Basic Class B virtual function in I ):


Class J object: Class J has a new virtual function based on class I. The object will store a vptr pointing to the new virtual function at the beginning of the memory. The memory layout is as follows:


2. Multi-Level inheritance (not multi-level inheritance !)

Due to the limited length, Here we only list two layers of virtual inheritance, and there are virtual functions in both the base class and the derived class for analysis. The UML class diagram is as follows:


The test results are as follows:


The memory layout is as follows:


1: stores the vptr_K pointer. The virtual function table pointed to by this pointer stores the newly added virtual function address.

2: stores the vbptr_k pointer. The virtual base table pointed to by this pointer stores the offset of the sub-objects of each base class relative to the address where vbptr_k is stored.

3: stores z_1, indicating the newly added data member of this class.

4: Save vptr_k_ B. The virtual function table pointed to by this pointer stores the deepest virtual function address in the base class.

5/6: stores a_1 and a_2, indicating the data member of the deepest base class.

7. Store vptr_K_J, and the virtual function table pointed to by this pointer is stored in the inheritance level added by Class J.

8. Store vbptr_K_J. The virtual base table pointed to by this pointer is stored in the inheritance level. The offset between the sub-objects of each base class in Class J and the address where vbptr_K_J is stored.

9. Store y_1, which indicates the data member of the J class.

In k object, it is worth noting that the sub-object part of Class B is stored before the sub-object part of Class J, which is done to achieve multi-inheritance sharing of the virtual base class.

Iii. Multi-Inheritance

The detailed discussion of Multi-inheritance is too complex. Here we refer to the two most basic situations. The memory layout can be learned based on the above two basic situations:

1. The virtual machine inherits from two classes.

After the test, the result is as follows:


We can see that the memory layout of class L objects is as follows:


2. Diamond inheritance: two classes are known to inherit from the same base class, and other classes inherit from two virtual classes. The UML class diagram is used to represent the following:


The test results are as follows:


The memory layout of M-type objects is as follows:


Summary:

This article analyzes the memory layout of class objects and the implementation of virtual functions, virtual inheritance, and multi-inheritance on memory allocation from three aspects: Non-inheritance class, single inheritance class, and multi-inheritance class. If any error occurs in the article, please leave a message. Thank you very much!


Note:

For more information, see http://blog.csdn.net/walkerkalr. Thank you for your cooperation!

If you need all source code, leave the email address.

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.