Skip to content
Tutorials Center
My learning

Topics

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.

C++
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

C++ What a new-expression really does

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.

C++ Failure handling, nothrow and arrays

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

C++
// 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 (or std::bad_array_new_length for an invalid array size) unless std::nothrow is used. A new-handler installed with std::set_new_handler is called first and may free memory and retry.
  • Mixing forms is undefined: delete on memory from new[], delete[] on memory from new, delete on malloc memory, or free on new memory.
  • 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 new is released automatically by the matching operator delete.
  • Replacing globally: you may define your own operator new/delete once 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 ::new to bypass them.
  • Placement new constructs an object in storage you already own and requires manual destruction; see new and delete.
  • Ownership: every new needs exactly one delete. Smart pointers (std::unique_ptr, std::make_shared) and containers do this for you, including during exceptions.
  • Size of arrays: new T[n] where n is a runtime value is fine; n of 0 returns a valid unique pointer you must still delete[].
  • Optimization: compilers may elide or merge allocations whose results are unobservable (C++14 allows it), so counting new calls in a test is unreliable.
  • Prefer std::vector<T>(n) over new T[n]; new is for implementing owners, not for application code.