<new>
void* operator new (std::size_t size) throw (std::bad_alloc); void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw(); void* operator new (std::size_t size, void* ptr) throw();
Allocate storage space
分配存储空间
The first version allocates size bytes of storage space, aligned to represent an object of that size, and returns a non-null pointer to the first byte of this block. On failure, it
throws a bad_allocexception.
第一个版本为对象分配size字节大小的存储空间,返回一个非空的指针指向这个内存块的第一个字节,当其失败的时候,抛出一个bad_alloc异常。
The second version is the nothrow version. It does the same as the first version, except that on failure it returns a null pointer instead of throwing an exception.
第二个版本和第一个版本基本一样,只不过其不抛出异常
The third version is the placement version, that does not allocate memory - it simply returns ptr. Notice though that the constructor for the object (if any) will still be called by the operator expression.
第三个版本是一种安置版本,其不分配内存,其仅仅返回ptr,注意到对象的构造函数(如果有的话)还是会被调用的。
Global dynamic storage operator functions are special in the standard library:
全局的动态存储操作函数在标准库里面是非同一般的
- All three versions of operator new are declared in the global namespace, not in the std namespace.
- 三个版本的operator new都是声明在全局命名空间,而不是在std
- The first and second versions are implicitly declared in every translation unit of a C++ program: The header <new> does not need to be included for them to be present.
- 第一和第二个版本隐性地声明在一个C++程序的每一个翻译单位中,头文件<new>不需要包括进来使他们存在。
- The first and second versions are also replaceable: A program may provide its own definition, that replaces the default one, to produce the result described above.
- 第一和第二个版本也是可以替换的:一个程序科技提供自己的定义,来替代默认的函数,来产生上面描述的结果。
If set_new_handler has been used to define a new_handler function, this new_handler function is called by the standard default definition of operator new if it cannot allocate the requested storage by its own.
如果set_new_handler被用来定义一个new_handler函数,那么new_handler函数将会被operator new调用,如果其不能分配所请求的内存调用的话。
operator new can be called explicitly as a regular function, but in C++, new is an operator with a very specific behavior: An expression with the new operator, first calls
operator new可以被显式的调用为一个规则的函数,但是在C++,new是一个具有特定的行为的函数:任何一个带有new操作符的表达式,其首先
function operator new with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed).
调用operator new,其中类型指定者的size作为其第一个参数,如果其是成功的,他将自动初始化或者构造对象。
Finally, the expression evaluates as a pointer to the appropriate type.
最终,该表达式求值为一个合理的类型的指针。
// operator new example
#include <iostream>
#include <new>
using namespace std;
struct myclass {myclass() {cout <<"myclass constructed\n";}};
int main () {
int * p1 = new int;
// same as:
// int * p1 = (int*) operator new (sizeof(int));
int * p2 = new (nothrow) int;
// same as:
// int * p2 = (int*) operator new (sizeof(int),nothrow);
myclass * p3 = (myclass*) operator new (sizeof(myclass));
// (!) not the same as:
// myclass * p3 = new myclass;
// (constructor not called by function call, even for non-POD types)
new (p3) myclass; // calls constructor
// same as:
// operator new (sizeof(myclass),p3)
return 0;
}