What is the C + + object model, the memory layout and structural problems of objects

Source: Internet
Author: User

The controversy over the C + + object model at the beginning of the C + + invention was never stopped until the standard committee passed the final C + + object model and the matter became dusty. The C + + object model is probably the least that needs to be explained, but it has to be said because C + + is the first thing that comes into contact with C + + objects. In the last century a total of three C + + object models, their appearance can be said to be a continuous optimization process ultimately only we see the C + + object model in use. It is important to understand the C + + object model, and it is very helpful to understand the memory layout, memory size, virtual functions, and the understanding of static data members and member functions of the object. To get to the bottom, we will discuss the three kinds of C + + object models (which should only be discussed at present, but not with the original obsolete comparison and how to reflect the current C + + object excellent). The following object models are described on the basis of this class

15.1 the simplest object model

Only the slot index is simply the use of an index slot, and each object, obj, has a large number of slot slots, which point to the data members and member functions of the class, although the time access efficiency is high but the memory cost is huge, which is intolerable in an era of expensive memory. Just like the figure below.

15.2 table-driven object model

A slightly better object model separates data from member functions, and in this object model, the data members and member functions of the class are divided into two different tables, one called DataMember table and the other called the function Table object itself, which is only a pointer to the two table. Just like in the picture below. Although this model is not used, function table provides a reference for the subsequent virtual function memory layout.

15.3 C + + Object Model

Now it's time to talk about our current C + + object model, now the object model is relatively complex the entire structure is divided into five regions, data member area, vptr virtual table driving pointer area, non-static function member area, static member region, The static function member area. It is important to note that only data members and vptr virtual table-driven pointers are stored in the object model's memory structure, just like.

Seeing this model can explain why static data members and member functions belong to classes and do not belong to an object because they have only one instance of the entire class. They don't exist inside the object. It is also possible to explain why the number of virtual functions in a class is always dropped to only 4 bytes of memory, because the virtual function has only one driver pointer to the virtual table. This is just two more typical examples, and there are plenty of other things to explain, such as the magical 1byte problem and so on.

What is the C + + object model, the memory layout and structural problems of objects

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.