Explore the C + + object model in depth-1

Source: Internet
Author: User

Overview

In the actual production, encountered a complex class, if you can see this kind of memory model structure, then the operation of the basic will not be difficult;

So, learning to analyze a class's memory model is a knowledge that every C + + programmer must have.

Below, let's get to know the C + + object Model!

The difference between C + + class encapsulation and struct in C

C + + class encapsulation is built on the structure of the C language, the C struct only allows the existence of data, and there is no manipulation of the data.

The C + + language extends the structure of the language, but increases the operation of the data, that is, the member function;

A class is a further encapsulation of a struct, making some data members invisible to the outside, known as private members.

The biggest difference between classes and structs is that struct members are of the public type.

So, are there any differences in the layout costs of classes and structs?

For classes and structs that have only data members, the layout of the memory is the same, and there is no additional cost.

The members functions, though contained within the class declaration, do not appear in object. each Non-inline member Fuction will only be born a function entity, which is linked to the function body when invoked. As for each inline function with 0 or one definition, a function entity is generated on each of its callers (modules).

In C + +, there are two classes of data members:static and nonstatic, and three classes member Functions:static, nonstatic, and virtual.

In the C + + object model, non-static data members are configured within each class object, while static data members are outside the individual class object , and static and non-static member functions are placed outside of the individual class object.

The main additional burden of C + + in layout and access time is caused by virtual. Including:

    1. virtual function mechanism: to support an efficient "execution period binding"
    2. Virtual base class is used to achieve "multiple occurrences of the base class in an integrated system, with a single, shared entity."

Virtual functions and virtual function tables either contain virtual functions or dummy inheritance, the class produces a virtual function table, and each object contains a vptr pointer to the virtual function table.

Implementation of virtual function

1. Each class generates a bunch of pointers to virtual functions, which are placed in the table, called virtual table (VTBL);

2. Each class object is inserted with a pointer to the associated virtual table. This pointer is often referred to as vptr. The vptr settings and resets are automatically completed by the constructors, destructors, and assignment operator overloads of each class and cannot be modified once the initialization is complete. The Type_info objectthat is associated with each class (primarily for Rtti) is also referred to by virtual table and is usually placed in the first slot of a table.

Multiple Inheritance Model

The data members in the inherited model time base class object originally adopted by C + + are stored directly in the inherited class object memory, including the virtual function table of the base class object and the virtual pointer (which looks like the current model), so that the base class members can be accessed most efficiently. However, the disadvantage is that if there are any modifications to the base class member, the base class object and the inheriting class object will be recompiled.

An indirect access model has been proposed: Place a pointer bptr in the inheriting class object, which points to the base class table of the inheriting class, similar to the virtual function table, where each item in the Base class table points to the base class object of the inheriting class object. The advantage of this is that any change to the base class object will not affect the memory layout of the inheriting class object, and the disadvantage is that the access to the base class object member is inefficient, with the increase of inheritance depth, the more indirect, the lower the efficiency.

As a result, it has been suggested that placing pointers to the equivalent number of base classes in the inheriting class object points to each base class so that the access time is constant, but this undoubtedly increases the memory overhead of the inheriting class.

The multiple inheritance model currently in use is still the first, inheriting class objects that contain all base class object data members and virtual pointers, while inheriting the virtual function table of the base class as a virtual function table of the inheriting class.

Virtual Inheritance Model

Virtual inheritance guarantees that only one base class object is contained in the inheriting class, and the inheriting class produces a virtual pointer to the virtual table in which offset points to the base class object in the inheriting class. only inherited class objects that have direct virtual inheritance will produce vptr.

Currently, there are three types of memory layouts for inherited classes of virtual inheritance:

1. Original model: Sets a pointer in each inherited class object to point to the base class object in the inheriting class. Advantage: The pointer ensures that there is only one base class object in the inheriting class, and that the base class object can be accessed directly. Disadvantage: If the inheriting class has more than one virtual base class, then multiple pointers are required to point to the base class, which consumes more memory.

2. As a virtual function table, it is proposed to provide a dummy base class table, in which each slot in the table points to the address of a base class object. In the inheriting class object, set a virtual base class table pointer to the base class table. Advantage: Inheriting classes reduce the overhead generated by virtual base class pointers, and inheriting classes do not change the memory model because of the increase in virtual base classes. Disadvantage: Accessing base class object data members can result in inefficiencies.

3, now the most commonly used models: the expansion of the original virtual table, in the negative offset position virtual tables point to the virtual base class object, so long as the virtual inheritance will produce virtual pointers and virtual tables.

Virtual multi-inheritance, using the "diamond" inheritance structure, each of its classes of memory distribution, such as: where the virtual pointer position can be at the top or tail.

The above content is an introduction to the class object memory model:

(1) No virtual function and virtual inheritance: Nostatic data members are stored in the object, and static data members reside outside the object and belong to the entire class. Nostatic and static function members are placed outside the object.

(2) There are virtual functions and virtual inheritance: The virtual function is placed in the virtual function table, and the lookup call is made by a virtual pointer. Both the base class object data member and the virtual pointer are stored in the object of the inheriting class.

Explore the C + + object model in depth-1

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.