Recommended reading: http://blog.csdn.net/randyjiawenjie/article/details/6693337
Recently, we have studied the memory object model inherited by C + +. Read http://blog.csdn.net/haoel/article/details/3081328 (memory layout of C + + objects ) mainly. This article is highly recommended.
Make a summary of this article. Much of this article comes from summarizing http://blog.csdn.net/haoel/article/details/3081328 (the memory layout of C + + objects ) in this article.
#类中的元素
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. Memory Alignment
The initialization of _vftptr and _vbtptr is done automatically by the constructor of the object, the assignment operator, and after the end of the object's life cycle, is destroyed by the object's destructor.
The type that the object is associated with (type_info), usually placed in the first slot of virtual table.
Virtual inheritance : The inheritance relationship of the virtual keyword is included in the inheritance definition;
virtual base class : The base class that inherits from virtual in the virtual inheritance system, it should be noted that:
Class Cderive:public virtual CBase {}; Where CBase is called the virtual base class of cderive, rather than saying that CBase is a virtual base class, because CBase can also be a base class that is not a virtual inheritance system.
After a virtual function is derived, it is still a virtual function, even if the virtual keyword is omitted from the derived class.
The construction and destruction of virtual base class is called by the final subclass (not directly derived subclass)
Note: "_vbptr is _vbtptr" below.
#对象内存布局分类讨论
VC6 in the variable viewer (LOCALS,WATCH1, etc.), you can also see the partial object layout (incomplete, and virtual inheritance is wrong).
VS2005 and later versions of the compiler provide the /d1reportsingleclasslayout[class name] compilation option to view the full memory layout of an object:
CL Classlayout.cpp/d1reportsingleclasslayoutcchildren
Note: The function in the class diagram example below is a virtual function ( italic indicates that the function is a virtual function)
0. Single class
(1). Empty class
sizeof (Cnull) =1 (used to identify the object)
(2). Class with member variables only
int nvarsize = sizeof (cvariable) = 12
Memory layout:
(3). Classes with only virtual functions
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 (with member variables, virtual functions, virtual function overrides)
int nchildsize = sizeof (Cchildren) = 12
Results shown in VC (note: There are also 1 virtual function cchildren::g1 not shown):
D1reportsingleclass View:
Memory layout:
2. Multiple inheritance (with member variables, virtual functions, virtual function overrides)
int nchildsize = sizeof (cchildren) = 20
Results shown in VC (note: There are also 2 virtual function cchildren::f2,cchildren::h2 not shown, this pointer's adjustor[adjustment value] also did not print out):
D1reportsingleclass View:
Memory layout:
3. Inheritance with a depth of 2 (with member variables, virtual functions, virtual function overrides)
int ngrandsize = sizeof (cgrandchildren) = 24
Results shown in VC (note: There are also 3 virtual functions cgrandchildren::f2,cchildren::h2,cgrandchildren::f3 not shown, the this pointer is adjustor[adjusted value] Also not printed):
D1reportsingleclass View:
Memory layout:
4 repeating inheritance (with member variables, virtual functions, virtual function overrides)
int ngrandsize = sizeof (cgrandchildren) = 28
VC Display Results (note: There are a large number of virtual functions are not shown, this pointer adjustor[adjustment value] also did not print out):
thunk function: A form real conversion auxiliary function, mainly to do this pointer adjustment, function call redirection.
D1reportsingleclass View:
Memory layout:
Since M_nage has two copies in the content, we cannot access the variable directly through Pgrandchildrena->m_nage.
This would be ambiguous, and the compiler would not know whether to access the m_nage in CChildren1 or M_nage in CChildren2.
In order to identify a unique m_nage, you need to bring the class name with its scope. As follows:
1 Pgrandchildrena->cchildren1::m_nage = 1;2 pgrandchildrena->cchildren2::m_nage = 2;
5. Single virtual inheritance (with member variables, virtual functions, virtual function overrides)
int nchildsize = sizeof (cchildren) = 20
D1reportsingleclass View:
Memory layout:
6. Multiple virtual inheritance (with member variables, virtual functions, virtual function overrides)
(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. Virtual multiple inheritance of diamond type (with member variables, virtual functions, virtual function overrides)
int ngrandchildsize = sizeof (Cgrandchildren) = 36
D1reportsingleclass View:
thunk function: A form real conversion auxiliary function, mainly to do this pointer adjustment, function call redirection.
Memory layout:
C + + Learning Note----Object memory model when 4.5 C + + Inherits