C ++ Object Memory Model [Memory layout], model layout

Source: Internet
Author: User

C ++ Object Memory Model [Memory layout], model layout

Source: http://www.cnblogs.com/kekec/archive/2013/01/27/2822872.html

# Elements in the class

0. member variables 1. member functions 2. static member variables 3. static member functions 4. virtual functions 5. Pure virtual functions

# Factors affecting the object size

0.Member variables1.Virtual function table pointer (_ vftptr)2.Virtual base table pointer(_ Vbtptr) 3.Memory alignment

_ Vftptr and _ vbtptr are initialized by the object constructor. The value assignment operator is automatically completed. After the object lifecycle ends, the object's destructor is destroyed.
The type (type_info) associated with the object is usually placed in the first slot of the virtual table.

Virtual inheritance: Contains the inheritance relationship of virtual keywords in the inheritance definition;
Virtual base class: In the virtual inheritance system, you must note the following:
Class CDerive: public virtual CBase {}; specifically, CBase is called the virtual base class of CDerive, rather than CBase, which is a virtual base class, because CBase can also be a base class not in the virtual inheritance system.

After a virtual function is derived, it is still a virtual function, even if the virtual keyword is omitted in the derived class.

Note: [below _ vbptr is _ vbtptr]

# Classification of Object Memory Layout

In the vc6 variable Viewer (Locals, wattings, etc.), you can also see the layout of some objects (incomplete, and false inheritance is incorrect ).

Vs2005 and later compilers provide/D1reportSingleClassLayout [class name]Compile the option to view the complete memory layout of the object:

cl classLayout.cpp /d1reportSingleClassLayoutCChildren

Note: The functions in the following example are virtual functions (ItalicsIndicates that this function is a virtual function)

0. single class

(1). Empty class

Sizeof (CNull) = 1 (used to identify this object)

(2). Class with only member variables

Int nVarSize = sizeof (CVariable) = 12

 

Memory layout:

(3). Class with only virtual functions

Int nVFuntionSize = sizeof (CVFuction) = 4 (virtual table pointer)

 

Memory layout:

(4). classes with member variables and virtual functions

 

Int nParentSize = sizeof (CParent) = 8

Memory layout:

1. Single inheritance (including member variables, virtual functions, and virtual functions)

Int nChildSize = sizeof (CChildren) = 12

Result displayed in vc (Note: There is another virtual function CChildren: g1 not displayed ):

D1reportSingleClass View:

Memory layout:

2. Multi-inheritance (including member variables, virtual functions, and virtual functions)

Int nChildSize = sizeof (CChildren) = 20

Results displayed in vc (Note: there are two other virtual functions CChildren: f2, CChildren: h2 not displayed, this pointerAdjustor[Adjusted value] is not printed either ):

D1reportSingleClass View:

Memory layout:

3. inheritance with a depth of 2 (including member variables, virtual functions, and virtual functions)

 

Int nGrandSize = sizeof (CGrandChildren) = 24

Results displayed in vc (note: There are three other virtual functions CGrandChildren: f2, CChildren: h2, CGrandChildren: f3 not displayed, this pointerAdjustor[Adjusted value] is not printed either ):

D1reportSingleClass View:

Memory layout:

4. Repeated inheritance (including member variables, virtual functions, and virtual functions)

 

Int nGrandSize = sizeof (CGrandChildren) = 28

Results displayed in vc (Note: a large number of virtual functions are not displayed, this pointerAdjustor[Adjusted value] is not printed either ):

Thunk function: A Real-form conversion helper function. It mainly adjusts the this pointer and redirects function calls.

D1reportSingleClass View:

Memory layout:

Because m_nAge has two copies in the content, we cannot directly access the variable through pGrandChildrenA-> m_nAge,

This will cause ambiguity. the compiler cannot know whether to access m_nAge in CChildren1 or m_nAge in CChildren2.

To identify the unique m_nAge, You need to include the class name in its range. As follows:

1 pGrandChildrenA->CChildren1::m_nAge = 1;2 pGrandChildrenA->CChildren2::m_nAge = 2;

5. Single virtual inheritance (including member variables, virtual functions, and virtual functions)

 

Int nChildSize = sizeof (CChildren) = 20

D1reportSingleClass View:

Memory layout:

 

6. Multi-virtual inheritance (including member variables, virtual functions, and virtual functions)

(1) virtual CParent1, CParent2

Int nChildSize = sizeof (CChildren) = 24

D1reportSingleClass View:

Memory layout:

(2) CParent1, virtual CParent2

Int nChildSize = sizeof (CChildren) = 24

D1reportSingleClass View:

Memory layout:

(3) virtual CParent1, virtual CParent2

Int nChildSize = sizeof (CChildren) = 28

D1reportSingleClass View:

Memory layout:

7. Diamond-Type Virtual Multi-inheritance (including member variables, virtual functions, and virtual functions)

Int nGrandChildSize = sizeof (CGrandChildren) = 36

D1reportSingleClass View:

Thunk function: A Real-form conversion helper function. It mainly adjusts the this pointer and redirects function calls.

Memory layout:

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.