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

c++中的new、operator new和placement new

1、new

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

new操做 会执行如下三个步骤ios

  1. 调用类的(若是重载了的话)或者全局的operator new分配空间
  2. 用类型后面列的参数列表来调用构造函数,生成类对象
  3. 返回对应的指针

2、 operator new

  • operator new是 operator 函数,与operator +等函数相似,能够被重载,operator new通常在类中进行重载。在全局重载容易形成程序崩溃,由于全局的::operator new 负责整个程序运行期间的堆空间的分配,重载全局::operator new 须慎之又慎!

例:c++

class T{
    ...
    void* operator new(size_t){
    ... //自定义操做
      return ::operator new(size_t);
  }
};

3、placement new

  • 是重载operator new 的一个标准、全局的版本缓存

  • 它不可以被自定义的版本代替,即不能重载。它的做用是在已分配的空间中构造对象。函数

void *operator new( size_t, void * p ) throw() { return p; }

Placement new使用步骤学习

在不少状况下,placement new的使用方法和其余普通的new有所不一样。这里提供了它的使用步骤。spa

  • 缓存提早分配
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:testNe  w::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;

}

  • 原创全部,转载注明出处,如有错误,欢迎你们指正,共同窗习。谢谢!
相关文章
相关标签/搜索