C++ Reference ยท Operators
The new and delete operators and allocation functions
Reference for new and delete expressions and the operator new/delete allocation functions: nothrow, placement, arrays, overloading and class-specific versions.
A new-expression such as new Widget(3) does two jobs: it calls an allocation function, operator new, to obtain raw memory, and then constructs an object in that memory. A delete-expression reverses it: it runs the destructor and then calls a deallocation function, operator delete. The expressions are fixed by the language; the allocation functions are ordinary functions you can replace or overload.
This page is about the operators and the functions behind them. For the keywords in everyday code, see new and delete; in modern C++ prefer std::make_unique, std::vector and other owners over raw new.
int* p = new int(5); // allocate + construct delete p; // destroy + deallocate int* a = new int[4]{}; // array form delete[] a; // must use delete[]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
// new-expressions new T new T(args) new T{args} new T[n] new T[n]{init} new (placement-args) T new (std::nothrow) T // delete-expressions delete p; delete[] p; // allocation functions (replaceable, <new>) void* operator new(std::size_t); void* operator new(std::size_t, std::align_val_t); // C++17, over-aligned void* operator new(std::size_t, const std::nothrow_t&) noexcept; void* operator new(std::size_t, void* ptr) noexcept; // placement, not replaceable void operator delete(void*) noexcept; void operator delete(void*, std::size_t) noexcept; // sized, C++14 void* operator new[](std::size_t); void operator delete[](void*) noexcept;
| Name | Description |
|---|---|
size |
Number of bytes requested (for arrays this may include a hidden count used by delete[]). |
align_val_t |
Alignment for over-aligned types; chosen automatically when the type needs it (C++17). |
std::nothrow |
Selects the overload that returns nullptr on failure. |
ptr |
Existing storage for placement new; nothing is allocated. |
Returns: operator new returns a pointer to suitably aligned storage; the new-expression returns a pointer to the constructed object.
Errors / undefined behavior:
- A failed allocation throws
std::bad_alloc(orstd::bad_array_new_lengthfor an invalid array size) unlessstd::nothrowis used. A new-handler installed withstd::set_new_handleris called first and may free memory and retry. - Mixing forms is undefined:
deleteon memory fromnew[],delete[]on memory fromnew,deleteonmallocmemory, orfreeonnewmemory. - Deleting twice, or using the object afterwards, is undefined. Deleting a null pointer is safe.
- Deleting a derived object through a base pointer without a virtual destructor is undefined.
Usage notes
- If the constructor throws, the memory from
operator newis released automatically by the matchingoperator delete. - Replacing globally: you may define your own
operator new/deleteonce in a program to count or pool allocations; define the matching deallocation functions too. - Class-specific versions (as in the example) are found first when you write
new Widget; use::newto bypass them. - Placement new constructs an object in storage you already own and requires manual destruction; see new and delete.
- Ownership: every
newneeds exactly onedelete. Smart pointers (std::unique_ptr,std::make_shared) and containers do this for you, including during exceptions. - Size of arrays:
new T[n]wherenis a runtime value is fine;nof 0 returns a valid unique pointer you must stilldelete[]. - Optimization: compilers may elide or merge allocations whose results are unobservable (C++14 allows it), so counting
newcalls in a test is unreliable. - Prefer
std::vector<T>(n)overnew T[n];newis for implementing owners, not for application code.