c++ 对象模型与内存结构
2017-03-21 18:50
411 查看
c++ Object model
object = data + algorithmData Layout
plain object
struct foo{ int a,b,c; };
12bytes
alignment
struct foo{ short a; int b; short c; };
short 2bytes
memory alignment 12bytes
inheritance
struct foo{ short a; int b; }; struct bar:foo{ short c; //short d; also 12bytes };
12bytes
4bytes alignment,foo 8bytes,c 4bytes
object in object
struct foo{ short a; int b; }; struct bar{ foo f; short d; };
12 bytes
static storage
layout : somewhere elsestatic member
functions
vtables
Virtual Binding
some conception
vtable:is the table containing address of Virtual Functions of each class.vptr:is the vpointer,which points to the Virtual Function for that object
virtual binding:A point or reference to an object calls virtual function
static binding:
An object calls function
A pointer or reference to an object or the object itself calls any non-virtual function
important points to remember
Only the Base class Method’s declaration needs the Virtual Keyword, not the definition.If a function is declared as virtual in the base class, it will be virtual in all its derived classes.
The address of the virtual Function is placed in the VTABLE and the copiler uses VPTR(vpointer) to point to the Virtual Function.
constuct order
Construct virtual base class(es)Construct base class(es)
Construct vptr(s)
Construct objects not in initialization list
Construct objects in initialization list
Call constructor
note
vptr is replaced again and again down the hierarchy treeVirtual function lose its virtuousness before the construction complete reguardless of static or dynamic binding
conclusion
the size of class’s memory
all the non-static member datamemory alignment
to support virtual function,it may need extra memory
Reference
c++ object model相关文章推荐
- C++对象模型笔记:对象的三种内存布局
- C++对象模型之内存区的使用
- [原创]C++虚继承内存对象模型探讨
- C++虚继承内存对象模型探讨
- 详细介绍c++中的类对象内存模型
- C++对象模型笔记:对象实例内存布局的小小结
- C++ - 对象内存布局模型
- 虚表结构与虚继承内存对象模型
- 详细介绍c++中的类对象内存模型
- C++对象模型(1):对象内存布局简介
- [C++对象模型][6]sizeof与对象内存布局
- c++对象模型 内存布局
- C++对象模型笔记:对象的三种内存布局
- 虚表结构与虚继承内存对象模型
- C++对象模型之内存区的使用
- [C++对象模型][6]sizeof与对象内存布局
- 深入理解C++对象模型-对象的内存布局,vptr,vtable
- C++对象内存结构之虚指针
- C++虚继承内存对象模型探讨
- c++对象内存模型【内存布局】