|
operator newfunction
<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_alloc exception.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. Global dynamic storage operator functions are special in the standard library:
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 the new 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. Parameters
Return valueFor the first and second versions, a pointer to the newly allocated storage space.For the third version, ptr is returned. Example
Output:
See also
|