The heap and smart pointers

new and delete by hand, then unique_ptr and shared_ptr, which make ownership part of the type.

What you'll learn
  • Allocate and free heap memory with new / delete and new[] / delete[]
  • Explain ownership, and choose between unique_ptr and shared_ptr
  • Transfer ownership with std::move

Local variables live on the stack and vanish when their function returns. For memory that must outlive a function, or whose size is only known at run time, there is the heap. 14dynamic_allocation.cpp does it by hand:

int *int_ptr = new int;          // one int
*int_ptr = 242;
delete int_ptr;

int *int_ptr1 = new int[10];     // an array of 10
int_ptr1[0] = 42;
delete[] int_ptr1;               // arrays need delete[]

Every new needs exactly one matching delete — not zero (a leak), not two (a double free, lesson 13), and never delete for something made with new[]. In a twenty-line program that is easy. In a program with early returns and exceptions it is not, which is why modern C++ almost never writes delete.

Ownership in the type

A smart pointer is a small object that owns a heap allocation and frees it in its destructor — automatically, on every path out of the scope.

Stack variables on the left, the heap block on the right. A unique_ptr can only be moved; a shared_ptr counts its owners in a control block; a raw pointer that is overwritten leaves its block orphaned.

std::unique_ptr is the default. It has exactly one owner, costs nothing over a raw pointer (sizeof is 8 bytes with g++ 13) and frees the memory when it goes out of scope:

auto ptr1 = std::make_unique<int[]>(10);
for (int i = 0; i < 10; i++) ptr1[i] = i * i;
std::cout << ptr1[4] << '\n';    // 16
std::cout << ptr1[7] << '\n';    // 49

It cannot be copied — two owners would both free the block — but it can be moved. 21move.cpp shows the transfer:

auto ptr2 = std::move(ptr1);
// ptr1 before move: 0x6258...32b0
// ptr1 after move:  0
// ptr2 after move:  0x6258...32b0

std::shared_ptr allows many owners. Next to the data it keeps a control block with a reference count; copying a shared_ptr increments it and destroying one decrements it, and the last owner frees the memory. 16shared_ptr.cpp copies once and prints Reference count: 2 twice. A shared_ptr is 16 bytes (data pointer plus control-block pointer) and every copy does an atomic increment, so it is not free — use it when ownership is genuinely shared.

Prefer make_unique and make_shared over new inside the constructor: make_shared puts the object and its control block in a single allocation, and neither leaves a raw new in the source to get wrong.

Where this shows up later

The snake game stores its game states as std::stack<std::unique_ptr<State>>, and moves each new state in with std::move — the ownership pattern above, at the scale of a real program. Lesson 7 builds the same idea into a class with a destructor.

Try it yourself. In 16shared_ptr.cpp, open an inner scope { auto r = ptr3; std::cout << ptr3.use_count(); } and print the count again after the closing brace. You should see 3 inside and 2 after. Then replace std::shared_ptr with std::unique_ptr and read the compiler error for auto ptr2 = ptr1;.

View source on GitHub →