小结:c++中的new、operator new和placement new
小结:c++中的new、operator new和placement new
- new(也称作new operator),是new 操作符,不可重载
class T{...};
T *t = new T(initial_args_list); //此时的new ,是new 操作符
new操作 会执行以下三个步骤
- 调用类的(如果重载了的话)或者全局的operator new分配空间
- 用类型后面列的参数列表来调用构造函数,生成类对象
- 返回对应的指针
- operator new 是operator 函数,与operator +类似,可以被重载,operator new一般在类中进行重载。在全局重载容易造成程序崩溃,因为全局的::operator new 负责整个程序运行期间的堆空间的分配,重载全局::operator new 须慎之又慎!
- operator new 在类中重载,完成自定义操作,并调用全局::operator new,返回对应的指针
例如:
class T{
...
void* operator new(size_t,args...){
... //自定义操作
return ::operator new(size_t);
}
};
- placement new 是重载operator new 的一个标准、全局的版本,它不能够被自定义的版本代替,即不能重载。它的作用是在已分配的空间中构造对象。
void *operator new( size_t, void * p ) throw() { return p; }
Placement new使用步骤
在很多情况下,placement new的使用方法和其他普通的new有所不同。这里提供了它的使用步骤。
- 缓存提前分配
char*buf = (::operator new((size_t)(sizeof(T))));
- 对象的分配,在已分配的缓存中调用构造函数,生成对象
T *t = new(buf)T;
- 使用对象,用-> 访问对象的成员
t->men_func();
- 调用外在的析构函数
t->~T();
- 释放资源
delete [] buf;
使用例子
#include <stdio.h>
#include <iostream>
#include <string>
#include <malloc.h>
using namespace std;
class testNew{
public:
testNew(){ cout << "执行了testNew::testNew() 构造函数" << endl; }
~testNew(){ cout << "执行了testNew::~testNew() 析构函数" << endl; }
void* operator new(size_t size, string str){
cout << "重载1:testNew::op new," << str << endl;
return ::operator new(size);
}
void* operator new(size_t size){
cout << "重载2:testNew::op new,without str" << endl;
return ::operator new(size);
}
void print(){
cout << "已初始化成功" << endl;
}
};
void * operator new(size_t size)
{
cout << "::op new 内存分配 "<< endl;
return malloc(size);
}
int main()
{
cout << "重载全局 ::op new" << endl;
char * buf = (char *)(::operator new((size_t)(sizeof(testNew))));
cout << endl;
cout << " placement new" << endl;
//不加::,会调用 void* testNew:: operator new(size_t size, string str)
//导致不能匹配全局的placement new
testNew *test = ::new (buf)testNew;
test->print();
test->~testNew();
delete []buf;
cout << endl;
cout << " 重载 testNew::op new 1" << endl;
//此时输出有4行
testNew *test2 = new("with str")testNew;
//::op new 内存分配 -> 给const char* "重载"分配堆空间
//重载1:testNew::op new,with str ->调用testNew::op new 1
//::op new 内存分配 ->testNew::op new 1调用 全局的 ::op new
//执行了testNew::testNew() 构造函数
test2->print(); //输出 “已初始化成功” ,表示已正确返回指针
cout << endl;
cout << " 重载 testNew::op new 2" << endl;
testNew *test3 = new testNew;
test3->print(); //输出 “已初始化成功” ,表示已正确返回指针
cout << endl;
cout << "析构" << endl;
delete test2;
delete test3;
getchar();
return 0;
}

- 原创所有,转载注明出处,若有错误,欢迎大家指正,共同学习。谢谢!
小结:c++中的new、operator new和placement new的更多相关文章
- C++中的new,operator new与placement new
以下是C++中的new,operator new与placement new进行了详细的说明介绍,需要的朋友可以过来参考下 new operator/delete operator就是new和 ...
- C++ 中 new 操作符内幕:new operator、operator new、placement new
一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...
- 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 ...
- 浅谈new operator、operator new和placement new 分类: C/C++ 2015-05-05 00:19 41人阅读 评论(0) 收藏
浅谈new operator.operator new和placement new C++中使用new来产生一个存在于heap(堆)上对象时,实际上是调用了operator new函数和placeme ...
- new 、operator new 和 placement new
一.原生operator new 我们先从原生operator new开始.考虑如下代码,它用来分配5个int型的空间并返回指向他们的指针[1]: int* v = static_cast<in ...
- C++ new operator, delete operator, operator new, operator delete, new placement
http://www.younfor.com/cpp-new-placement-new-operator-new.html http://www.cnblogs.com/luxiaoxun/arch ...
- 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new
引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...
- [C++空间分配]new运算符、operator new、placement new的区别于联系
先科普一下: 1. new的执行过程: (1)通过operator new申请内存 (2)使用placement new调用构造函数(内置类型忽略此步) (3)返回内存指针 2. new和malloc ...
随机推荐
- 说说VNode节点(Vue.js实现)
写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出.文章的原地址:https://github.com/an ...
- 微信小程序AES加密解密
微信小程序 其实在调用一些第三方 js的时候 其实没有大家想的那么复杂,无非就是把原生js调用方式 改为微信小程序 js调用方式 废话不多说直接贴代码 其实就是将原生function 或者 对象 ...
- Java经典编程题50道之二十二
利用递归方法求5!. public class Example22 { public static void main(String[] args) { int n = 5; ...
- shell 颜色控制系列
shell脚本里,经常用的颜色控制,如下 格式:echo -e "\033[字背景颜色:文字颜色m字符串\033[0m" eg:echo -e "\033[41;36m ...
- IM开发基础知识补课:正确理解前置HTTP SSO单点登陆接口的原理
1.前言 一个安全的信息系统,合法身份检查是必须环节.尤其IM这种以“人”为中心的社交体系,身份认证更是必不可少. 一些PC时代小型IM系统中,身份认证可能直接做到长连接中(也就是整个IM系统都是以长 ...
- Qt 动态加载DLL 常见错误有哪些?
1. dll 路径不对,比如 IE 中 2. 依赖库缺失,会报错找不到指定模块 注意: qt 的 qlibrary 只能加载 标准 C 函数
- es6之let和const命令的一些笔记
let和const命令 let命令 基本用法 let命令用来声明变量,声明的变量只在命令所在的代码块内有效.for循环中很适合使用let命令. 有必要理解的例子: var a = []; for (v ...
- string (KMP+期望DP)
Time Limit: 1000 ms Memory Limit: 256 MB Description 给定一个由且仅由字符 'H' , 'T' 构成的字符串$S$. 给定一个最初为空的字符串 ...
- 电脑太卡怎么解决-IT33
首先我们看一下引起电脑卡顿的原因有哪些: 1. 电脑可能感染木马病毒. 2. 硬盘使用时间过长,硬盘有坏道. 3. 软件开太多导致内存不足. 4. 电脑磁盘中冗余或者碎片过多. 5. ...
- javascript form表单常用的正则表达式
form验证时常用的几个正则表达式 座机: \d{3,4}-\d{7,8} 手机号: /^1[34578][0-9]{9}$/ (\86)?\s+1[34578]\d{0-9} (\+86)?\s*1 ...