c++中new/operator new/placement new
1. new/delete
c++中的new(和对应的delete)是对堆内存进行申请和释放,且两个都不能被重载。
2. operator new/operator delete
c++中如果想要实现不同的内存分配行为,需要重载operator new,operator delete。operator new和operator +一样是可以重载的,但是不能在全局对原型为 void* operator new(size_t size)这个原型进行重载,一般只能在类中进行重载(operator new)。同理,operator new[], operator delete, operator delete[]也是可以重载的,一般在重载了其中一个之后,最好将其他三个都重载一遍。
class MyClass{
....
};
MyClass* p = new MyClass;
这里的new实际执行三个过程:
(1)调用operator new分配内存
(2)调用构造函数构造类对象
(3)返回指针
在类中可以对operator new进行重载,如果类中没有重载operator new,则在new一个对象的时候,调用的是全局的::operator new来完成堆的分配。
3. placement new
placement new 是operator new的一个重载版本,placement new允许在一个已经分配好的内存块(堆或者栈)中创建一个对象。原型中void*p实际指向一个已经分配好的内存缓冲区的首地址。
new操作符分配内存需要在堆中查找足够大的剩余空间,这个操作速度很慢,而且有可能出现无法分配内存的异常。placement new构造对象都是在一个预先准备好的内存缓冲区中进行,不需要查找内存,内存分配的时间为常数;而且不会出现在程序运行中途出现内存不足的异常。所以,placement new适合那些对时间要求比较高,长时间运行不希望被打断的应用程序。
placement new的使用方法:
(1)缓冲区提前分配
可以使用堆空间,也可以使用栈空间。
class MyClass{
...
};
char* buf = new char[N*sizeof(MyClass) + sizeof(int)];
或者
char buf[N*sizeof(MyClass) + sizeof(int)];
(2)对象的构造
MyClass* pClass = new (buf) MyClass; //placement new
(3)对象的销毁
一旦对象使用完毕,必须显式的调用析构函数来销毁对象。但此时内存空间不会被释放,以便其他对象的构造。
pClass->~MyClass(); //显式调用析构
(4)内存的释放
如果缓冲区在堆中,则调用delete[] buf进行释放;如果在栈中,当跳出作用域之后,内存自动释放。
虽然内置类型没有构造函数,但也可以使用placement new
int* i = (int*)malloc(sizeof(int));
new (i) int();
注意
在C++标准中,对于placement operator new []有如下的说明: placement operator new[] needs implementation-defined amount of additional storage to save a size of array. 所以我们必须申请比原始对象大小多出sizeof(int)个字节来存放对象的个数,或者说数组的大小。
c++中new/operator new/placement new的更多相关文章
- C++内存管理(new operator/operator new/operator delete/placement new)
new operator 我们平时使用的new是new操作符(new operator),就像sizeof一样是语言内置的,不能改变它的含义,功能也是一样的 比如: string *ps = new ...
- Python中使用operator模块实现对象的多级排序
Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...
- 【转】C++易混知识点3. New Operator, Operator New, Placement New 实例分析,比较区别
我们知道,C++中引入了New 这个内置符号,很大方便了指针的使用,程序员不必关注与这块堆上新分配的内存是如何来的,如何初始化的,然后如何转换为我们想要的类型指针的.现在,我们重点来分析下这个NEW内 ...
- 关于python中的operator.itemgetter()函数的用法
1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...
- C++中的::operator new, ::operator delete
一般在使用new 和 delete的时候,做了两件事情,一是空间的配置( new 是分配,delete是回收),而是调用对象的析构函数 但是也有办法将这两个过程分开 那就是显式的调用::operat ...
- python中的operator.itemgetter函数
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号.看下面的例子 a = [,,] >>> b=) ...
- c++中的new、operator new、placement new
一.定义 1.new new是c++中的关键字,,其行为总是一致的.它先调用operator new分配内存,然后调用构造函数初始化那段内存. new 操作符的执行过程:1. 调用operator n ...
- C++中的new、operator new与placement new
转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator ...
- C++中的new,operator new与placement new
以下是C++中的new,operator new与placement new进行了详细的说明介绍,需要的朋友可以过来参考下 new operator/delete operator就是new和 ...
随机推荐
- [script]判定某一个脚本是否正确执行
返回内容存在$?, 0代表成功,其他代表失败 some_command ]; then echo OK else echo FAIL fi 字符型的返回值 some_command retval=$? ...
- Python idle安装与使用教程 调试、下载
Python idle安装与使用教程 调试.下载 今天我们就来讲一下如何安装Python idle编辑器,也它的调试和使用. 第一步,我们先去下载一个Python idle程序安装包. 本节讲的是wi ...
- 完美解决方案:wordpress后台进不去,用户名、密码输入了登陆没有反应(有更新)
http://mingming4745.blog.163.com/blog/static/173845720119159425890/?suggestedreading ——————————————— ...
- EasyUi---searchbox 条件查询
前台UI参考代码: <script type="text/javascript" charset="utf-8"> $(function(){ /* ...
- thikphp 简单的接口骨架
//get id 获取内容,调用xml方法 public function get(){ $id = $_GET['id'];//接收id $User = M('user'); //$val-> ...
- 关于C++输出中文乱码的解决方案
把页面编码转换为UTP-8的编码 1.打开G:\vs2013way\VC\vcprojectitems目录 在 file.h newc++file.cpp 中写两句话 #pragma once#pra ...
- python cython 模块(1)
python 是一门动态类型的语音,其开发速度比C,C++等静态语言块, 但是速度慢很多, 而cython 通过混合C和python 的语法,可以提高python代码的运行速度 1) 安装cython ...
- 10种canvas鼠标光标动画特效
来源:http://www.sucaihuo.com/js/1780.html demo:http://www.sucaihuo.com/jquery/17/1780/demo/
- office2003 下载地址 及密码
http://www.downxia.com/downinfo/63.html microsoft office 2003 密钥 GWH28-DGCMP-P6RC4-6J4MT-3HFDY Micro ...
- 高级类特性----抽象类(abstract class)
抽象类(abstract class) 随着继承层次中一个个新子类的定义,类变得越来越具体,而父类则更一般,更通用.类的设计应该保证父类和子类能够共享特征.有时将一个父类设计得非常抽象,以至于它没有具 ...