The heap: new and delete
Local variables live on the stack and vanish when their function returns. When data must outlive a function, or its size is only known while the program runs, you ask the heap for memory with new and give it back with delete. Follow a nine-line program as new returns addresses that pointers keep, *p follows one to change a value, delete leaves a dangling pointer, and a forgotten delete becomes a leak.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What does int* p = new int(42); store in p?
new int(42) makes an unnamed int on the heap, puts 42 in it and returns its address. p, a local variable on the stack, keeps that address; *p is the 42.
02Which line correctly frees memory made by new int[3]{1, 2, 3}?
Memory from new[] must go back with delete[]. Using plain delete on it (or delete[] on memory from plain new) is undefined behaviour.
03After delete p; what is true about p?
delete frees the memory p points to but does not change p. p is now a dangling pointer. Setting p = nullptr after delete is a common safety habit.
04A function calls new int(5), keeps the address only in a local pointer, and returns without delete. What happens?
The local pointer disappears with the stack frame, but heap memory stays until someone deletes it. With no pointer left, nobody can: that is a leak.
05Why do the C++ Core Guidelines advise against writing new and delete yourself?
Guideline R.11 says to avoid explicit new and delete. A std::unique_ptr or std::vector frees its memory when it goes out of scope (RAII), which prevents leaks and double deletes.
Glossary
- Stack
- The memory for a running function's local variables. They are made when the function starts and removed automatically when it returns.
- Heap (free store)
- Memory you ask for while the program runs with new, and give back with delete. It lasts until you free it.
- new
- An expression that reserves memory on the heap for an object (or an array with new[]), initialises it and returns its address.
- delete / delete[]
- Gives heap memory back. Use delete for memory from new, and delete[] for memory from new[].
- Pointer
- A variable that holds an address, such as int* p. *p means the object at that address.
- Dangling pointer
- A pointer that still holds the address of memory that has been freed. Using it is undefined behaviour.
- Memory leak
- Heap memory that was never freed and can no longer be reached, because no pointer to it remains.
- Undefined behaviour
- Code the C++ standard places no requirements on: it may seem to work, give wrong results or crash.
Notes and sources
- The C++ standard calls this memory the free store; most people say the heap. It is not the heap data structure from algorithm books. new and delete get their memory from it through operator new and operator delete.
- The addresses 0x5a10 and 0x5a30 are made up and short so they fit. Real ones are longer (for example 0x55d4c3a1b2c0 on 64-bit Linux), change from run to run, and blocks usually have gaps and bookkeeping bytes between them.
- An int is 4 bytes on the usual desktop platforms (64-bit Linux, macOS and Windows), which is why a[1] sits 4 bytes after a[0]. The standard only promises at least 16 bits.
- Using memory after delete, deleting twice, or mixing delete with new[] is undefined behaviour: the program may seem to work, print garbage or crash. Tools such as AddressSanitizer (-fsanitize=address in GCC and Clang) catch many of these.
- In modern C++ you rarely write new and delete yourself: the C++ Core Guidelines (R.11) say to avoid them. std::unique_ptr, std::make_unique and std::vector own heap memory and free it automatically when they go out of scope (RAII). Knowing new and delete explains what they do for you.
- The program on the card is real, standard C++ (C++11 or later). Build it with g++ -std=c++20 -Wall -Wextra heap.cpp -o heap and run ./heap: it prints 46.
- cppreference.com: new expression
- cppreference.com: delete expression
- cppreference.com: Storage duration (automatic and dynamic)
- cppreference.com: std::unique_ptr
- cppreference.com: Fundamental types (size of int)
- Standard C++ Foundation (isocpp.org): Freestore management FAQ
- C++ Core Guidelines R.11: Avoid calling new and delete explicitly
- Clang documentation: AddressSanitizer
Quiz Unlocks after the last step.