Namespaces
Variants
Views
Actions

new expression

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
new expression
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Initializes objects in dynamically obtained memory.

Used where data need to be allocated dynamically, that is, without knowing its size prior the compilation.

Contents

[edit] Syntax

::(optional) new type [array_n](optional) (init_params)(optional) (1)
::(optional) new type [array_n](optional) (init_params)(optional) (2)
::(optional) new (placement_params) type [array_n](optional) (init_params)(optional) (3)
::(optional)    new      (placement_params)     type     [array_n](optional)     (init_params)(optional)     (4)

All versions return an object of type type *.

[edit] Explanation

The new expression allocates a memory area, initializes either single object, or an array of objects there and returns a pointer to the first constructed object. Since it is not otherwise possible to call explicitly call a constructor, the expression is the only way to construct an object dynamically.

The first two and last two versions of the expression differ only syntactically. The behavior is the same.

[edit] Memory allocation

The memory is allocated by an allocation function, either operator new or operator new[].

The last two versions of the expression are called placement new and are used to pass additional parameters (placement_params) to the allocation function.

If array_n is absent, memory is allocated for a single object by invoking operator new allocation function. Otherwise memory for an array of array_n objects is allocated by calling operator new[] allocation function. array_n shall be a compile-time integer constant. Note, that more than size_of( type ) * array_n might be allocated because of additional information encoded by the compiler (such as the size of the array, since this information is needed in order to destruct the objects in the array properly).

The allocation function's name is firstly looked up in the local class type scope and only if the lookup fails, the global namespace is looked up. If :: is present in the new expression, only the global namespace is looked up. The prototype of the function should look like the following in order to the allocation function to be selected:

void* operator new  (size_t count);
for versions 1, 2, array_n is not present
void* operator new[](size_t count);
for versions 1, 2, array_n is present
void* operator new  (size_t count/*, placement_params...*/);
for versions 3, 4 (placement new), array_n is not present
void* operator new[](size_t count/*, placement_params...*/);
for versions 3, 4 (placement new), array_n is present

count is number of bytes to allocate, placement_params are the parameters given to the placement new expression.

[edit] Default implementations and overloading

Several default allocation functions are implicitly declared in each translation unit. Also, implicit implementations are provided by the compiler by default, unless the program has explicitly implemented them. These functions are as follows (see this for more information):

void* operator new  (size_t);
(1)
void* operator new[](size_t);
(2)
void* operator new  (size_t, std::nothrow_t);
(3)
void* operator new[](size_t, std::nothrow_t);
(4)
void* operator new  (size_t, void* ptr);
(5)
void* operator new[](size_t, void* ptr);
(6)

1-2) allocates requested number of bytes or throws std::bad_alloc on failure

3-4) allocates requested number of bytes or returns NULL on failure

5-6) returns ptr. This allows to construct an object in user-supplied memory area. The program must not define explicit implementations for these functions.

[edit] Object initialization

If array_n is absent, single object is initialized in the acquired memory area, passing init_params as parameters to the constructor or invoking default constructor if they are not present.

If array_n is present, an array of array_n objects is initialized, passing init_params as parameters to the constructor of each object or invoking default constructor if init_params are not present.

[edit] Keywords

new