Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

Dynamic memory in C++: new, delete and arrays

How dynamic memory works in C++: free store versus stack, new and delete, arrays, operator new, malloc comparison, leaks, dangling pointers and safer options.

Dynamic memory is storage whose lifetime you control instead of the scope. C++ gets it from the free store (the heap) with new and gives it back with delete. Local variables live on the stack and vanish automatically at the end of their block; free-store objects stay until you release them.

This page is about the concept and its hazards. The syntax of the expressions themselves is on new and delete. In day-to-day code you should let std::vector, std::string and smart pointers manage this memory for you.

Try it

C++ Stack versus free store, and the failure modes

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++ Arrays on the free store

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.

Main ideas

Pairs. new T pairs with delete, new T[n] with delete[], malloc with free. Crossing the pairs is undefined behavior. Memory from new must not be passed to free, or the reverse.

What new does. It calls the allocation function operator new(size_t), then runs the constructor. If allocation fails it throws std::bad_alloc; see Allocation failures. delete runs the destructor, then calls operator delete. malloc does neither construction nor destruction, so it is only suitable for trivial types.

Lifetime of the pointer versus the object. The pointer variable is a separate object. Destroying the pointer does not free what it points to; that is how leaks happen.

Usage notes

  • Leak: the last pointer to an allocation is lost (reassigned, or out of scope) without delete. Memory use grows. Tools: AddressSanitizer and LeakSanitizer.
  • Dangling pointer: a pointer remains after delete. Using it is undefined behavior; it may even seem to work.
  • Double delete: deleting the same address twice is undefined behavior. Deleting nullptr is harmless.
  • Exceptions: Counter* c = new Counter; mayThrow(); delete c; leaks if mayThrow() throws. This is why raw owning pointers are discouraged.
  • Ownership: document or encode who deletes. A raw T* in modern code means "borrowed, may be null", never "you must delete this".
  • Arrays of unknown size: a pointer from new[] does not remember its size, and sizeof(ptr) is the pointer's size. Use std::vector or std::unique_ptr<T[]> plus a length variable.
  • Cost: each allocation takes time and carries bookkeeping bytes. Allocating inside a hot loop is slow; reserve once or reuse buffers.
  • Alignment: new respects over-aligned types since C++17.
  • Cleaner alternatives: local variables, std::vector, std::string, std::array, std::make_unique, std::make_shared.