为了满足应用程序对内存分配的特殊需求,C++允许重载new运算符和delete运算符控制内存分配,通过定位new表达式初始化对象(好处是可以在某些场景下避免重新内存分配的消耗)
1、operate new /delete
#include "QtGuiApplication2.h" class A { public: A():m_i(0){} A(int i):m_i(i){} ~A(){} void* operator new(size_t st) noexcept { if (void *mem = malloc(st)) { return mem; } return nullptr; } void operator delete(void *mem) noexcept { free(mem); } private: int m_i; }; QtGuiApplication2::QtGuiApplication2(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); A* a = new A(3);//new表达式走了3步:1、调用operate new分配内存 2、调用构造函数 3、返回一个A对象指针
delete a;//delete表达式走了2步:1、调用析构函数 2、释放空间 }
2、定位new表达式什么时候需要呢
#include "QtGuiApplication2.h" class A { public: A():m_i(0){} A(int i):m_i(i){} ~A(){} void* operator new(size_t st) noexcept { if (void *mem = malloc(st)) { return mem; } return nullptr; } void operator delete(void *mem) noexcept { free(mem); } private: int m_i; }; QtGuiApplication2::QtGuiApplication2(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); void* a = A::operator new(sizeof(A)); for (int i = 0; i < 1000; ++i) { A* placement = ::new (a) A(i); //dosomething to placement... placement->~A(); } A::operator delete(a); }
假设在1000次循环中就可以省去每次分配和释放内存空间的消耗了
定位new的形式:
new (place_address) type
new (place_address) type (initializers)
new (place_address) type [size]
new (place_address) type [size] {braced initializer list}
总结:
重载new和delete可以自定义内存分配方式,而定位new表达式则提供构造对象的途径(记得给place_address传一个分配好空间的void指针才行哦)