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.
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
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 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:
newwithdelete,new[]withdelete[]. Mixing them is undefined behavior, and so is deleting the same pointer twice or deleting a pointer you did not get fromnew. delete nullptris safe and does nothing. Setting a pointer tonullptrafterdeletemakes accidental reuse fail loudly, but ownership types are a better cure.new intleaves the value indeterminate;new int()ornew int{}zero-initializes. Reading an indeterminate value is undefined behavior.newcallsoperator new(allocation) and then the constructor;deletecalls the destructor and thenoperator 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)andstd::make_shared<T>(args),std::vectorfor arrays andstd::stringfor text. Then there is nothing to leak when an exception is thrown. - Over-aligned types (
alignas(64)) are handled correctly bynewsince C++17. - Placement
newonly 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).