operator new, operator new
Defined in header
void* operator new ( std::size_t count );
void* operator new( std::size_t count );
void* operator new ( std::size_t, void* ptr );
void* operator new( std::size_t, void* ptr );
Allocates requested number of bytes. These allocation functions are called by new-expressions to allocate memory in which new object would then be initialized.
countbytes 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.
ptr. These versions are called by new-expression which construct objects in previously allocated storage.
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 per program may be provided for each of the four implicit allocation functions. Also, a 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
void* operator new (size_t count/*, placement_params*/);
| for the
void* operator new(size_t count/*, placement_params*/);
| for the
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 newmust 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
For more information see
Per name lookup rules, any allocation function declared in class scope hides all global allocation functions.
For each allocation function, at most one global replacement may be provided in the entire program, and that one replacement is automatically used by all allocations made through that function in the rest of the program, with no changes to the code.
Even though placement new (overloads 5 and 6) cannot be replaced, a function with the same signature may be defined at class scope and selected by name lookup unless the new expression used
::new. Besides, global overloads that look like placement new but take a non-void pointer type as the second argument are allowed, so the code that wants to ensure that the true placement new is called (e.g. std::allocator::construct), must use
::new and cast the pointer to
It is not possible to place allocation function in a namespace.
|count||-||number of bytes to allocate|
|ptr||-||pointer to a memory area to initialize the object at|
Custom operator new can be used for any purpose, for example, to fill the allocated array
| deallocation functions |
| obtains the current new handler |
| registers a new handler |
| obtains uninitialized storage |
| allocates memory |