c++ 对象模型与内存结构
来源:互联网 发布:东华 数据库 编辑:程序博客网 时间:2024/06/15 00:45
c++ Object model
object = data + algorithm
Data 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 else
- static 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 tree
Virtual 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 data
- memory alignment
- to support virtual function,it may need extra memory
Reference
c++ object model
0 0
- c++ 对象模型与内存结构
- 虚表结构与虚继承内存对象模型
- 虚表结构与虚继承内存对象模型
- 虚表结构与虚继承内存对象模型
- Java内存结构与模型
- 对象生命周期与内存模型
- 虚表结构与虚继承内存对象模型(gcc vs vc)
- 【C++】C++对象内存模型简介
- 漫谈C++:对象内存模型分析
- C++-对象继承内存模型配图
- C语言结构体对象内存对齐
- c/c++结构体、对象内存对齐
- Java内存模型与对象的探秘
- 详解java内存模型与对象
- jvm 内存模型结构
- Java内存结构模型
- [C++对象模型][6]sizeof与对象内存布局
- [C++对象模型][6]sizeof与对象内存布局
- ES6学习笔记
- AndroidAutoLayout
- SCI-Hub
- C++类中的六个默认函数(二)
- JDK中的Timer和TimerTask详解
- c++ 对象模型与内存结构
- 室内定位之蓝牙Beacon-部署方案
- path与classpath区别
- css三种样式表区别,优先级。
- java3的 DateUtils方法
- ACM程序设计题目 Problem N-14
- 管理
- windows下 eclipse for scala+spark+mongodb
- 机器学习入门:线性回归及梯度下降(一)