Operator new函数

operator 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 abad_alloc exception.

第一个版本分配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.

第二个版本是一个nothrow版本。基本操作与第一个版本相同,除了当分配失败时,它返回一个空指针而不是抛出异常。


The third version is the placement version, that does not allocate memory - it simply returnsptr. Notice though that the constructor for the object (if any) will still be called by the operator expression.

第三个版本是placement new版本,它不分配内存--它只返回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 thestd namespace.
  • 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.
  • 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.

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 thenew operator, first calls 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). Finally, the expression evaluates as a pointer to the appropriate type.

operator new函数可以作为一个正常的函数进行显式调用,但是在C++里,new是一个非常特别的操作符,对于一个new操作表达式,首先调用函数operator new,并且利用对应类型的size大小初始化第一个参数,如果成功,那么它会自动调用对象的构造函数,然后表达式返回适当类型的指针。

注:

在C++中,new操作符可以被重载接收多于一个参数:第一个传递给operator new的参数总是元素类型的分配的内存大小,但是其它的参数可以通过括号进行传递,举例来说:

 
int * p = new (x) int;


这个表达式将会调用


operator new (sizeof(int),x);



Parameters

size
Size in bytes of the requested memory block.
size_t is an integral type.
nothrow_constant
The constant nothrow.
This parameter is only used to distinguish it from the first version with an overloaded version. When the nothrow constant is passed as second parameter to operator new, operator new returns a null-pointer on failure instead of throwing a bad_alloc exception.
nothrow_t is the type of constant nothrow.
ptr
A pointer to a memory block where the object is to be constructed

Return value

For the first and second versions, a pointer to the newly allocated storage space.

For the third version, ptr is returned.


// 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;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值