C + + Object memory Model "Memory layout"

Source: Internet
Author: User

#类中的元素

0. Member variable 1. member function 2. static member Variable 3. static member function 4. virtual function 5. Pure virtual function

#影响对象大小的因素

0. Member variable 1. virtual function table pointer (_VFTPTR) 2. Virtual base class table pointer (_VBTPTR) 3. Snap to memory

_VFTPTR, _vbtptr initialization is done automatically by the object's constructor, the assignment operator, and the object's lifecycle is destroyed by the object's destructor.
The type (type_info) associated with the object, usually placed in the first slot of the virtual table.

Virtual inheritance: Contains the inheritance relation of the virtual keyword in the inheritance definition;
Virtual base class: The base class that inherits from virtual in the virtual inheritance system, which should be noted:
Class Cderive:public virtual CBase {}; Among them, CBase called the virtual base class of cderive, not that CBase is a virtual base class, because CBase can also be a base class in a virtual inheritance system.

When a virtual function is derived, it is still a virtual function, even if the virtual keyword is omitted from the derived class.

Note: "Below _vbptr namely _vbtptr"

#对象内存布局分类讨论

In the VC6 variable viewer (LOCALS,WATCH1, etc.), you can also see the partial layout of the object (incomplete, and the virtual inheritance is incorrect).

The vs2005 and later compilers provide the/d1reportsingleclasslayout[class name] Compilation option to view the complete memory layout of the object:

CL Classlayout.cpp/d1reportsingleclasslayoutcchildren

0. Single class

(1). Empty class

sizeof (Cnull) =1 (used to identify the object)

(2). Classes with only member variables

int nvarsize = sizeof (cvariable) = 12

Memory layout:

(3). Classes with virtual functions only

int nvfuntionsize = sizeof (cvfuction) = 4 (virtual table pointer)

Memory layout:

(4). Classes with member variables, virtual functions

int nparentsize = sizeof (cparent) = 8

Memory layout:

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

int nchildsize = sizeof (Cchildren) = 12

VC Display Results (note: There are 1 virtual functions cchildren::g1 not shown):

D1reportsingleclass View:

Memory layout:

2. Multiple inheritance (including member variables, virtual functions, virtual function overrides)

int nchildsize = sizeof (cchildren) = 20

VC Display Results (Note: There are 2 virtual functions cchildren::f2,cchildren::h2 is not displayed, the this pointer adjustor[adjustment value] is not printed):

D1reportsingleclass View:

Memory layout:

3. The depth of 2 of the inheritance (including member variables, virtual functions, virtual function overrides)

int ngrandsize = sizeof (cgrandchildren) = 24

VC Display Results (Note: There are 3 virtual functions cgrandchildren::f2,cchildren::h2,cgrandchildren::f3 not shown, this pointer's adjustor[adjustment value] is not printed):

D1reportsingleclass View:

Memory layout:

4 Repeat inheritance (including member variables, virtual functions, virtual function overrides)

int ngrandsize = sizeof (cgrandchildren) = 28

VC Display Results (note: There are also a large number of virtual functions do not appear, this pointer's adjustor[adjustment value] does not print out):

thunk function: A form real transformation auxiliary function; this pointer adjustment, function call redirection.

D1reportsingleclass View:

Memory layout:

Because M_nage has two copies in the content, we cannot access the variable directly through Pgrandchildrena->m_nage.

There is ambiguity, and the compiler cannot know whether to access the m_nage in CChildren1 or the m_nage in CChildren2.

To identify a unique m_nage, you need to bring the class name of its scope. As follows:

1 pgrandchildrena->cchildren1::m_nage = 1;
2 pgrandchildrena->cchildren2::m_nage = 2;

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

int nchildsize = sizeof (cchildren) = 20

D1reportsingleclass View:

Memory layout:

6. Multi-Virtual inheritance (including member variables, virtual functions, virtual function overrides)

(1) Virtual CParent1, CParent2

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.