Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

new — creating objects and allocating storage

A new-expression creates an object and returns a pointer to it; it usually also obtains storage. Distinguish it from the allocation function operator new, and learn about array new[] and the Placement new-expression that constructs an object in supplied storage.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

new T(args...)
new T{args...}
new T[n]{}
::new (address) T{args...}
new (std::nothrow) T{args...}

If T is not an array type, new T{args...} creates and initializes an object, returning a T*. A new expression is not the same as the function operator new: the latter supplies raw storage but does not itself call a constructor. The initializer matters: new int leaves an indeterminate value that must not be read in C++23; new int{} produces zero.

The array form new[], such as new T[n]{}, creates an array. On success, if n > 0, it returns a pointer to the first element. In C++23, n == 0 is valid: the array has no elements, so the returned pointer must not be dereferenced. Match ordinary single-object new with delete, and new that creates an array with delete[], even for a zero-length array. For example, after using T = int[3];, new T{} returns an int* to the first element. The array must still be deleted with delete[], even though the new-expression contains no square brackets. Mixing these forms causes undefined behavior. Deleting objects does not reset pointers holding their addresses.

The standard throwing allocation function throws std::bad_alloc when it cannot allocate storage. If new (std::nothrow) T{} selects the standard non-throwing allocation overload, the expression returns nullptr on allocation failure. If T is a class type, the expression can instead select a class-specific allocation overload that accepts std::nothrow_t but can still throw. Even with the standard non-throwing overload, the expression can propagate a constructor exception. Check the result before dereferencing it. If a constructor throws, the new expression calls a suitable deallocation function if one is found.

A Placement new-expression of the form ::new (address) T{args...}, using the standard void* overload from <new>, constructs an object in existing storage without allocating it. The supplied address must not be nullptr, and the storage must be large enough, correctly aligned, and available throughout the object's lifetime. For the example's buffer, call placed->~Item(), not delete placed; the buffer has its own lifetime.

Entry scope: C++23. The associated repository compiler flag does not attest the production runtime. The supplied standard URL points to an evolving working draft, not a frozen C++23 edition.

Examples

#include <cstddef>
#include <iostream>
#include <new>

struct Item {
    int value;
};

int main() {
    int* single = new int{7};
    std::cout << *single << '\n';
    delete single;

    int* array = new int[3]{1, 2, 3};
    std::cout << array[0] + array[1] + array[2] << '\n';
    delete[] array;

    alignas(Item) std::byte storage[sizeof(Item)];
    Item* placed = ::new (static_cast<void*>(storage)) Item{9};
    std::cout << placed->value << '\n';
    placed->~Item();
}

Result

7
6
9

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources