Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The new and delete expressions

Reference for C++ new and delete: allocating single objects and arrays, new[] versus delete[], nothrow and placement forms, and why smart pointers are preferred.

new allocates memory from the free store, constructs an object (or an array of objects) there, and returns a pointer to it. delete runs the destructor and releases the memory. Together they let an object outlive the scope that created it.

In modern C++ you rarely write them directly. std::make_unique, std::make_shared and containers do the same job and release memory automatically. Learn the raw forms to read older code and to understand what the smart pointers do for you.

C++
int* p = new int{42};        // allocate and initialize one int
delete p;                    // destroy and free
int* a = new int[10]{};      // ten zeroed ints
delete[] a;                  // arrays need delete[]

Try it

C++ Allocating objects 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.

C++ nothrow, placement new and the failure mode

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 T                      // default-initialized (fundamental types: indeterminate value)
new T()                    // value-initialized
new T{args}                // list-initialized
new T[n]  /  new T[n]{...} // array of n objects
new (std::nothrow) T       // returns nullptr on failure
new (address) T(args)      // placement new
delete ptr;                // single object (or nullptr)
delete[] ptr;              // array allocated with new[]
Part Description
T Type to allocate. For arrays only the first dimension may be a runtime value
n Number of elements (may be zero; a valid pointer is still returned)
ptr Pointer that came from the matching new form, or null

Returns: a pointer to the new object (T*). Errors: throws std::bad_alloc if memory runs out (std::bad_array_new_length for an invalid array size). The nothrow form returns nullptr instead. A throwing constructor frees the memory automatically before the exception continues.

Usage notes

  • Match the forms: new with delete, new[] with delete[]. Mixing them is undefined behavior, and so is deleting the same pointer twice or deleting a pointer you did not get from new.
  • delete nullptr is safe and does nothing. Setting a pointer to nullptr after delete makes accidental reuse fail loudly, but ownership types are a better cure.
  • new int leaves the value indeterminate; new int() or new int{} zero-initializes. Reading an indeterminate value is undefined behavior.
  • new calls operator new (allocation) and then the constructor; delete calls the destructor and then operator delete. You can replace these functions globally or per class to track or pool memory.
  • Deleting an object through a base pointer needs a virtual destructor; otherwise behavior is undefined.
  • Prefer std::make_unique<T>(args) and std::make_shared<T>(args), std::vector for arrays and std::string for text. Then there is nothing to leak when an exception is thrown.
  • Over-aligned types (alignas(64)) are handled correctly by new since C++17.
  • Placement new only constructs; it does not allocate. You are responsible for storage, alignment and calling the destructor.
  • Leaks, double frees and use-after-free are found by AddressSanitizer (-fsanitize=address).