Namespaces
Variants
Views
Actions

operator new, operator new[]

From cppreference.com
< cpp‎ | memory‎ | new
Revision as of 15:51, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <new>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
void* operator new  ( std::size_t count );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
void* operator new[]( std::size_t count );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
void* operator new  ( std::size_t count, const std::nothrow_t& );
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
void* operator new[]( std::size_t count, const std::nothrow_t& );
</td>

<td > (4) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
void* operator new  ( std::size_t, void* ptr );
</td>

<td > (5) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
void* operator new[]( std::size_t, void* ptr );
</td>

<td > (6) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Allocates requested number of bytes. These allocation functions are called by new-expressions to allocate memory in which new object would then be initialized.

1-2) Allocates count bytes from free store. Calls the function pointer returned by std::get_new_handler on failure and repeats allocation attempts until new handler does not return or becomes a null pointer, at which time throws std::bad_alloc.

3-4) Same as 1-2, but returns a null pointer when 1-2 would throw std::bad_alloc

5-6) does nothing, returns ptr. These versions are called by new-expression which construct objects in previously allocated storage.

Contents

Replacing and overloading

The versions 1-4) are implicitly declared in each translation unit even if the <new> header is not included. These functions are replaceable: a user-provided non-member function with the same signature replaces the implicit version. At most one replacement may be provided for each of the four implicit allocation functions. Also, program can define class member versions of these functions or define allocation functions with different signatures (except that it is not permitted to replace (5-6) versions of the allocation function). The added signature should look like the following, where count is number of bytes to allocate and placement_params are the parameters supplied to the new expression:

Template:ddcl list begin <tr class="t-dcl ">

<td >
void* operator new  (size_t count/*, placement_params*/);
</td>

<td class="t-dcl-nopad"> </td> <td > for the new version </td> </tr> <tr class="t-dcl ">

<td >
void* operator new[](size_t count/*, placement_params*/);
</td>

<td class="t-dcl-nopad"> </td> <td > for the new[] version </td> </tr> Template:ddcl list end

The allocation function can be replaced/overloaded in two ways:

in the global scope: in order to call it, the signature of the overloaded allocation functions must be visible at the place of allocation, except for implicitly declared default allocation functions. This allocation function will be used for all allocations with corresponding parameters in the current program
in the local scope: the overloaded operator new must be static public member function of the class. This allocation function will be used only for allocations of that particular class.

During compilation, each new expression looks up for appropriate allocation function's name firstly in the class scope and after that in the global scope. It can be instructed to skip the first step by calling new as ::new. Note, that as per overloading rules, any allocation functions declared in class scope hides all global allocation functions. For more information see new expression. Note, that it is not possible to place allocation function in a namespace.

Parameters

count - number of bytes to allocate
ptr - pointer to a memory area to initialize the object at

Return value

1-4) pointer to allocated memory area

5-6) ptr

Exceptions

1-2) throws std::bad_alloc on failure to allocate memory

3-6)
noexcept specification:  
noexcept
  

See also

Template:cpp/memory/new/dcl list operator deleteTemplate:cpp/memory/new/dcl list get new handlerTemplate:cpp/memory/new/dcl list set new handler
obtains uninitialized storage
(function)
allocates memory
(function)