小结: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 ...
 
随机推荐
- 数据库之mac上mysql root密码忘记或权限错误的解决办法
			
[转自 http://blog.csdn.net/u014410695/article/details/50630233] 以下方法亲测有效,过程使用的工具只有mac的终端无需workbench 当 ...
 - win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程
			
操作系统:Microsoft Windows 7 旗舰版(32位) 数据库版本:SQL Server 2005 简体中文开发板 数据库下载链接: https://pan.baidu.com/s/1cq ...
 - kvm克隆
			
virt-clone --original aming2 --name aming3 --file /data/kvm/aming3.qcow2 相关的克隆命令 克隆前必须关闭虚拟机 virs ...
 - hexo博客简易搭建教程
			
什么是Hexo Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页.官网 Hexo安装 安装 在安装Hex ...
 - js使用defineProperty的一些坑
			
var p2={ }; Object.defineProperty(p2,"gs",{ get:function () { return this.gs; }, set:funct ...
 - 编程中&和&&的区别
			
逻辑电路中用&: 与门电路,全真为真,有假为假. 编程中:&表示取地址符(C)和 按位与(非bool类型时,转换成二进制,按位与运算). &&表示逻辑与运算,& ...
 - poj2635 同余定理 + 素数筛法
			
题意:给定一个数,这个数是两个素数的乘积,并给定一个限制L,问是否两个素数中存在小于L的数,若存在输出较小质数,否则打印'GOOD'. 思路: 1 . x = a * b, a和b都是素数,那么x只能 ...
 - mysql的常用引擎
			
在MySQL数据库中,常用的引擎主要就是2个:Innodb和MyIASM. 首先: 1.简单介绍这两种引擎,以及该如何去选择.2.这两种引擎所使用的数据结构是什么. 1. a.Innodb引擎,Inn ...
 - Luogu P1747 好奇怪的游戏
			
题目背景 <爱与愁的故事第三弹·shopping>娱乐章. 调调口味来道水题. 题目描述 爱与愁大神坐在公交车上无聊,于是玩起了手机.一款奇怪的游戏进入了爱与愁大神的眼帘:***(游戏名被 ...
 - ORA-04028: cannot generate diana for object xxx
			
在ORACLE数据库(10.2.0.5.0)上修改一个包的时候,编译有错误,具体错误信息为"ORA-04028: cannot generate diana for object xxx&q ...