小结:c++中的new、operator new和placement new

  • new(也称作new operator),是new 操作符,不可重载
class T{...};
T *t = new T(initial_args_list); //此时的new ,是new 操作符

new操作 会执行以下三个步骤

  1. 调用类的(如果重载了的话)或者全局的operator new分配空间
  2. 用类型后面列的参数列表来调用构造函数,生成类对象
  3. 返回对应的指针
  • 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的更多相关文章

  1. C++中的new,operator new与placement new

    以下是C++中的new,operator new与placement new进行了详细的说明介绍,需要的朋友可以过来参考下     new operator/delete operator就是new和 ...

  2. C++ 中 new 操作符内幕:new operator、operator new、placement new

    一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...

  3. c++中的new、operator new、placement new

    一.定义 1.new new是c++中的关键字,,其行为总是一致的.它先调用operator new分配内存,然后调用构造函数初始化那段内存. new 操作符的执行过程:1. 调用operator n ...

  4. C++中的new、operator new与placement new

    转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator ...

  5. 浅谈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 ...

  6. new 、operator new 和 placement new

    一.原生operator new 我们先从原生operator new开始.考虑如下代码,它用来分配5个int型的空间并返回指向他们的指针[1]: int* v = static_cast<in ...

  7. 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 ...

  8. 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new

    引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...

  9. [C++空间分配]new运算符、operator new、placement new的区别于联系

    先科普一下: 1. new的执行过程: (1)通过operator new申请内存 (2)使用placement new调用构造函数(内置类型忽略此步) (3)返回内存指针 2. new和malloc ...

随机推荐

  1. POJ 1021 2D-Nim

    Description The 2D-Nim board game is played on a grid, with pieces on the grid points. On each move, ...

  2. Head First C 笔记

    嗨翻c语言 1. 入门 为什么字符从零开始编号? 字符的索引数值表示的是一个偏移量,它表示的是当前所引用的字符与第一个字符之间差多少个字符. 单双引号的区别? 单引号 一个字符,双- 字符串 字符串字 ...

  3. Java经典编程题50道之六

    输入两个正整数m和n,求其最大公约数和最小公倍数. public class Example06 {    public static void main(String[] args) {       ...

  4. ios设备触发虚拟键盘输入后position:fixed 无效的一些简单另类的解决方法。

    首先看一下我要解决的问题,第一张图是正常的情况下,第二张图是点击了输入框之后的情况,就是要解决此问题~! 百度了一下解决方法,好像有以下的一些方法: 1. iscroll 2. Jquery Mobi ...

  5. UVA - 11292 Dragon of Loowater 贪心

    贪心策略:一个直径为X的头颅,应该让雇佣费用满足大于等于X且最小的骑士来砍掉,这样才能使得花费最少. AC代码 #include <cstdio> #include <cmath&g ...

  6. hdu4825 01字典树+贪心

    从高位向低位构造字典树,因为高位得到的数更大. AC代码: #include<cstdio> using namespace std; typedef long long LL; cons ...

  7. 【BZOJ2959】长跑 (LCT+并查集)

    Time Limit: 1000 ms   Memory Limit: 256 MB Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室 ...

  8. php-fpm问题

    这个问题怎么说呢?之前遇到这个问题内心是奔溃的.因为我压根不知道是哪里出问题啦.不过,在我努力探索下,最终还是解决了问题. so请记住,坚持不一定成功,但放弃一定失败. 简单描述一下问题: 1.本地的 ...

  9. shiro整合ehcache

    目标:让Shiro整合ehcache,提供缓存realm数据的功能. 1.引入encache配置文件,配置缓存 <!-- <ehcache xmlns:xsi="http://w ...

  10. java-数据库连接,分层实现增删改查测试

    成员属性类: public class Dog { private int number; private String name; private String strain; private St ...