Caffeinated Coder
Book an appointment
Lesson 10 · Memory

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.

12 min9 steps5 questionsIntermediate
Fig 10 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.