How `new’ operator works ?
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=15
February 15, 2013
How `new’ operator works ?
(original works by Peixu Zhu)
For single inherited classes
1. in case of single instance of a class without virtual method (inherited or not).
- suppose the class is `theClass‘.
- at first, it calls function `malloc‘ to allocate sizeof(theClass) memory, the size is always the same to POD structures.
- if the `malloc‘ function fails, throw exception if `nothrow‘ is not specified.
- if the `malloc‘ function success, call class initializer and internal initializer to set default values of members( to be zeros)
- call the constructor on the instance as a chain, the most rooted constructor is called at first, and then the derived constructors, the latest is theClass‘s constructor.
- set return value to be the address of `malloc‘ returned.
2. in case of single instance of a class with virtual method (inherited or not).
- at first, it calls functon `malloc‘ to allocate sizeof(theClass) sized memory, for single inherited class, sizeof(theClass) = (sizeof(void*) ) + sizeof(POD structure). the additional (sizeof(void*) ) sized memory is for purpose of storing virtual pointer table for the class and it’s parent classes.
- if the `malloc‘ function fails, throw exception if `nothrow‘ is not specified.
- if the `malloc‘ function success, at first, set the first (sizeof(void*) to be the class’s virtual pointer table, and then call class initializer and internal initializer to set default values of members( to be zeros) on subsequent memory.
- call the constructor on the instance as a chain, the most rooted constructor is called at first, and then derived classes’ constructors, the latest is the class’s constructor.
- set return value to be the address of `malloc‘ returned.
3. in case of arrayed instances of a class without virtual method.
- suppose n instances are required.
- calculate the size of required memory: sizeof(void*) + n * sizeof(theClass)
- call `malloc‘ to allocate memory of the size required.
- if `malloc‘ fails, throw exception if `nothrow‘ is not specified.
- if `malloc‘ success, set the first sizeof(void*) the count of instances in the array (i.e. `n’).
- for subsequent memory, each instance is initialized and constructed as above .
- set return value to be the address of `malloc‘ returned minus sizeof(void*), i.e., the address of first instance.
4. in case of arrayed instances of a class with virtual method.
- calculate the size of required memory:
sizeof(void*) + n * sizeof(theClass).
sizeof(theClass) = sizeof(void*) + sizeof(POD)
- call `malloc‘ to allocate the size required.
- if `malloc‘ fails, throw exception if `nothrow‘ is not specified.
- if `malloc‘ success, set the first sizeof(void*) the count of instances in the array (i.e. `n’).
- for subsequent memory, for each instance, set the first
sizeof(void*) memory to be the address of virtual pointer table of the
class, then initialize the members, and call constructor one by one. - set return value to be the address of `malloc‘ returned minus sizeof(void*), i.e., the address of first instance.
5. about the virtual pointer table.
- the layout of virtual pointer table:
[vdes1][vdes2](vm1)(vm2)(vm3…)[typeinfo [data of typeinfo]].
square bracketing indicates optional.
each elements are pointer to functions/methods.
vdes1 and vdes2 are virtual destructor.
vm1/vm2 … are virtual methods.
typeinfo for function `typeid‘ (std::type_info)
data of typeinfo is the data of std::type_info
- if the class is virtual, then there is typeinfo, and data of typeinfo.
- if the class has virtual desctructor, there’s vdes1 and vdes2. one is called by `delete‘ operator (free memory in function), and ther other one is called by `delete[]‘ operator (does not free memory in function).
- in runtime environment, calling virtual methods are converted into
referencing index in the virtual pointer table, the index value of each
virtual method is determined at compiling time. In derived classes, the
child class instance and parent class instance share the same index
value on same virtual method (with same mangling signature). If the
child class does not override the parent virtual method, it will set the
indexed pointer to the parent’s method, rather than of the child,
however, if the child class override the virtual method, it will be the
indexed with pointer to the child’s method.
6. about the alignment of address returned by `new/new[]’ operator.
As you know, `new‘ and `new[]‘ operator both call `malloc‘ function to allocate memory required, thus, the alignment of address returned by `new‘ or `new[]‘ is determined by the address returned by `malloc‘. `malloc‘ does not guarantee the returned address is aligned well, thus, the `new‘ and `new[]‘ also do not guarantee the returned address is aligned well, though the size of the class is aligned.
But, there’s alignment version of `malloc‘, like posix_memalign, or valloc, etc., how about alignment version of `new‘ and `new[]‘ ? The replacement new operator in C++11 may solve the problem.
For classes with multiple parent classes.
1. each parent class has an instance in the derived class instance, sequenced as the class definition.
2. the derived class’s own members are placed at the tail of the allocated memory.
3. sizeof(theClass) = sizeof(parentClass) * (count of parent classes) + sizeof(own)
4. for plain classes without virtual methods, sizeof(own) = sizeof(POD structure), and for classes with virtual methods, sizeof(own) = sizeof(void*) + sizeof(POD structure).
How `new’ operator works ?的更多相关文章
- CLR via C# 3rd - 04 - Type Fundamentals
1. System.Object The runtime requires every type to ultimately be derived from the System.Obj ...
- Flink - Working with State
All transformations in Flink may look like functions (in the functional processing terminology), but ...
- Think Python - Chapter 11 - Dictionaries
Dictionaries A dictionary is like a list, but more general. In a list, the indices have to be intege ...
- MDX : Non Empty v/s NonEmpty
MDX : Non Empty v/s NonEmpty User Rating: / 50 PoorBest Written by Jason Thomas Friday, 07 May 20 ...
- NonEmpty和Non Empty的区别[转]
One of my favourite questions in MDX is the difference between Non Empty and NonEmpty because even t ...
- JsonPath详解
JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...
- C++11 : variadic templates(可变参数模板)
Introduction: Before the possibilities of the new C++ language standard, C++11, the use of templat ...
- ADF BC New Features
Examining ADF Business Components New Features Purpose In this tutorial, you create a series of si ...
- Dart语言特性必备了解!
学习Dart语言,必须将以下的概念熟记于心: 在dart语言中,一切皆为对象.所有的对象都是一个类的实例.甚至整数.函数.null也看做是对象.所有的对象都继承于Object类 尽管Dart是强类型语 ...
随机推荐
- html5--6-23 CSS3中的文字与字体
html5--6-23 CSS3中的文字与字体 text-overflow 设置是否使用一个省略标记(...)标示对象内文本的溢出 clip: 默认值当对象内文本溢出时不显示省略标记(...),而是将 ...
- iOS中键盘的收起
在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法之外,还有另外三种方法: 重载UIViewController中的touchesBegin方 ...
- VS2008 MFC截取整个屏幕并保存为jpg格式
void CMainFrame::OnSavejpg() { // TODO: 在此添加命令处理程序代码 HWND hwnd = this->GetSafeHwnd(); //得到窗口句柄 HD ...
- Table View Programming Guide for iOS---(七)---Managing Selections
Managing Selections 管理选择 When users tap a row of a table view, usually something happens as a result ...
- 016--python文件处理
一.操作文件流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 示例代码: f = open('chenli.txt') #打开文件 first_line = ...
- (水题)Codeforces - 630H - Benches
https://codeforces.com/problemset/problem/630/H 又一个组合数学的问题,我们先考虑在 $n$ 列中选出 $5$ 列来放椅子,然后对第一列有 $n$ 种放法 ...
- ARC和MRC混合使用
在一些项目中尤其是做迭代的项目经常会出现MRC的项目,但是我们习惯了ARC环境,反之也是一样.这是我们不必去修改代码去掉release之类的,按照如下方案去做就可以了. 项目 -> Build ...
- lightoj 1025【区间DP】
题意: 给出一个word,求有多少种方法你从这个word清除一些字符而达到一个回文串. 思路: 区间问题,还是区间DP: 我判断小的区间有多少,然后往外扩大一点. dp[i,j]就代表从i到j的方案数 ...
- hdu2767(图的强连通)
//题意:问需要添加几条边使得这张图成为每个点都等价(强连通图) 我们先把图中的强连通分量缩点 可能他本身就是满足条件,那么直接输出0 经过缩点后,就可以把强连通分量看成一个个独立的点,在这张图上搞一 ...
- 2018 年度码云热门项目排行榜 TOP 10
2016 年度码云热门项目排行榜 TOP 10 是通过开源项目2016年在码云上的 Watch.Star.Fork 数量来评定的榜单.码云平台发展至今,涌现了越来越多优秀的开源项目,越来越多的开源作者 ...