c++11 里 new 操作符的用法有以下几种,摘自cplusplus.com
throwing (1) void* operator new (std::size_t size);
nothrow (2) void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
placement (3) void* operator new (std::size_t size, void* ptr) noexcept;
平时用得最多的应该就是第(1)个,类似 xx *p = new xx,例子:
#include <iostream> // std::cout
#include <new> // ::operator new
#include <malloc.h>
struct MyClass
{
int data[10];
MyClass(int value)
{
std::cout << "constructed [" << this << "]\n";
for(int i = 0; i < 10; i++)
{
data[i] = value;
}
}
~MyClass()
{
std::cout << "destructed [" << this << "]\n";
}
};
int main ()
{
std::cout << "1: ";
MyClass * p1 = new MyClass(0);
// allocates memory by calling: operator new (sizeof(MyClass))
// and then constructs an object at the newly allocated space
std::cout << "2: ";
MyClass * p2 = new (std::nothrow) MyClass(1);
// allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
// and then constructs an object at the newly allocated space
std::cout << "3: ";
void *ptr = new (p2) MyClass(22);
std::cout << "3-1: ";
std::cout << "ptr = " << ptr << ", ptr size: " << malloc_usable_size(ptr) << std::endl;
// does not allocate memory -- calls: operator new (sizeof(MyClass),p2)
// but constructs an object at p2
// Notice though that calling this function directly does not construct an object:
std::cout << "4: ";
MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass));
// allocates memory by calling: operator new (sizeof(MyClass))
// but does not call MyClass's constructor
std::cout << std::endl;
delete p1;
delete p2;
delete p3;
return 0;
}
第3种用法见得不多,它是直接返回 p2,并且在 p2 所指的位置上构造 了 MyClass 对象。
可以看到 ptr 所指向的地址和 p2 的地址是一样的,因为返回的就是 p2 的地址。
可以看到在内存 0x603040 开始的 10 个4字节上,初始化了MyClass对象,即调用了MyClass构造函数。其实用法 void* operator new (std::size_t size, void* ptr) 中的 ptr 可以像上面的一样是堆内存,也可以是栈内存(即不是malloc/new 出来 的),如:
#include <iostream> // std::cout
#include <new> // ::operator new
#include <malloc.h>
struct MyClass
{
int data[10];
MyClass(int value)
{
std::cout << "constructed [" << this << "]\n";
for(int i = 0; i < 10; i++)
{
data[i] = value;
}
}
~MyClass()
{
std::cout << "destructed [" << this << "]\n";
}
};
int main ()
{
int tempMem[50] = {0};
std::cout << "1: ";
MyClass * p1 = new MyClass(0);
// allocates memory by calling: operator new (sizeof(MyClass))
// and then constructs an object at the newly allocated space
std::cout << "2: ";
MyClass * p2 = new (std::nothrow) MyClass(1);
// allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
// and then constructs an object at the newly allocated space
std::cout << "3: ";
void *ptr = new (p2) MyClass(22);
std::cout << "3-1: ";
std::cout << "ptr = " << ptr << ", ptr size: " << malloc_usable_size(ptr) << std::endl;
// does not allocate memory -- calls: operator new (sizeof(MyClass),p2)
// but constructs an object at p2
std::cout << "3-2: ";
void *ptr2 = new (tempMem) MyClass(33);
std::cout << "3-3: ";
std::cout << "ptr2 = " << ptr2 << std::endl;
// constructs an object at stack
// Notice though that calling this function directly does not construct an object:
std::cout << "4: ";
MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass));
// allocates memory by calling: operator new (sizeof(MyClass))
// but does not call MyClass's constructor
std::cout << std::endl;
delete p1;
delete p2;
delete p3;
return 0;
}
加了如下代码 int tempMem[50] = {0}; void *ptr2 = new (tempMem) MyClass(33);
可以看到无论是在堆内存还是栈内存上对象都是能正常构造的,只要有足够的空间。
总结:这种在指定内存上创建对象有什么用处呢?其实它是用在内存池方案中(或者说是伙伴算法实现的内存管理),一般c++实现的的大型项目中是不允许直接用 malloc 或 new 进行内存申请的,都是有指定的类,这个类所申请的内存就是从一大块内存里的某些块里分配的,然后在这指定的内存块上构造了这个指定的类,然后返回给用户使用,在析构的时候再把那块内存还给那一大块内存(不用释放),这样可以有效的减少内存碎片的发生。