new expression

Creates and initializes objects with dynamic storage duration, that is, objects whose lifetime is not necessarily limited by the scope in which they were created.

# Notes

Itanium C++ ABI requires that the array allocation overhead is zero if the element type of the created array is trivially destructible. So does MSVC.

Some implementations (e.g. MSVC before VS 2019 v16.7) require non-zero array allocation overhead on non-allocating placement array new if the element type is not trivially destructible, which is no longer conforming since CWG issue 2382.

A non-allocating placement array new expression that creates an array of unsigned char, or std::byte(since C++17) can be used to implicitly create objects on given region of storage: it ends lifetime of objects overlapping with the array, and then implicitly creates objects of implicit-lifetime types in the array.

std::vector offers similar functionality for one-dimensional dynamic arrays.

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 74C++98value in the first dimension must have integral typeenumeration types permitted
CWG 299C++98value in the first dimension musthave integral or enumeration typeclass types with a singleconversion function to integralor enumeration type permitted
CWG 624C++98the behavior was unspecified when thesize of the allocated object would exceedthe implementation-defined limitno storage is obtained and anexception is thrown in this case
CWG 1748C++98non-allocating placement new needto check if the argument is nullundefined behavior for null argument
CWG 1992C++11new (std::nothrow) int[N]could throw std::bad_array_new_lengthchanged to return a null pointer
CWG 2102C++98it was unclear whether default/value-initialization isrequired to be well-formed when initializing empty arraysrequired
CWG 2382C++98non-allocating placement array newcould require allocation overheadsuch allocation overhead disallowed
CWG 2392C++11the program might be ill-formed even if thefirst dimension is not potentially-evaluatedwell-formed in this case
P1009R2C++11the array bound could not bededuced in a new expressiondeduction permitted

# See also