C ++ Object Memory Structure

Source: Internet
Author: User

For simple objects, the memory structure layout is very simple and follows the following description:

1. Non-static data members are the main factors affecting the memory size occupied by objects, which increases with the increase of objects;

2. All objects share a static data member. When a sizeof is used to calculate an object, it will not contain static data members to occupy the memory size;

3. Static and non-static member functions do not affect the object memory size;

4. If the object contains a virtual function, four bytes of space will be added to save the virtual function table pointer.

 

The following describes a single inheritance class:

There is a base class instance in the instance header of the derived class, and the virtual function table shares the same base class. The memory layout of the base class is the same as that of the simple object. The memory layout of the derived class is the memory layout composed of the memory layout of the base class and the members added by the derived class, at the same time, you only need to modify the virtual function table of the base class without adding the virtual function table pointer. For a class, the entireProgramThere is only one virtual function table, regardless of the number of objects.

 

Multi-inheritance is basically the same as single inheritance, with only one exception, when a derived class is derived from two parent classes with a common base class, two instance objects of the parent class of the derived class will be created. The solution is to use virtual inheritance, virtual inheritance can generate only one parent class instance.

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.