Document directory
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 allocatesSizeBytes 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.
The first version allocates a size byte of storage space, and the returned value points to the first byte of the allocated space. If the allocation fails, a bad_alloc exception is thrown.
The second version isNothrow version. It does the same as the first version, doesn't that on failure it returns a null pointer instead of throwing an exception.
The second version is a nothrow version. The basic operation is the same as the first version. Except when the allocation fails, it returns a null pointer instead of throwing an exception.
The third version isPlacement 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.
The third version is placement new, which does not allocate memory -- it only returns PTR.Note: The object constructor will still be called.
Global Dynamic Storage operator functions are special in the Standard Library:
- All three versionsOperator newAre declared in the global namespace, not inSTDNamespace.
- The first and second versions areImplicitly declaredIn every translation unit of a C ++ program: The Header<New>Does not need to be encoded for them to be present.
- The first and second versions are alsoReplaceable: 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 anew_handler function, this
New_handlerFunction is called by the standard default definition
Operator newIf it cannot allocate the requested storage by its own.
Operator newCan be called explicitly as a regular function, but in C ++,NewIs an operator with a very specific behavior: an expression withNewOperator, first CILS Function
Operator newWith 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 functions can be explicitly called as a normal function, but in C ++, new is a very special operator. For a new operation expression, first, call the function operator new and use the size of the corresponding type to initialize the first parameter. If the parameter is successful, it automatically calls the object's constructor and then returns the appropriate type of pointer to the expression.
Note:
In C ++, the new operator can be overloaded to receive more than one parameter: the first parameter passed to operator New is always the allocated memory size of the element type, however, other parameters can be passed through brackets. For example:
This expression will call
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 thenothrow constant is passed as second parameter Operator new,
Operator newReturns a null-pointer on failure instead of throwing abad_alloc exception.
Nothrow_t is the type of constantnothrow.
-
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,PTRIs 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;}