0 day streak
4. Languages and runtimesC++ memory and objects

C++ memory and objects

C++ is a language that lets you decide where objects live in memory and exactly when they’re cleaned up. A local variable lives on the stack and is destroyed the moment its scope ends; an object made with new lives on the heap until something deletes it. Each class can have a destructor that runs at that precise moment, and the core idiom, RAII, ties resources like memory, files or locks to an object’s lifetime so they’re released automatically, even when an exception is thrown. Smart pointers build on this: unique_ptr means one owner, shared_ptr counts owners. That control makes C++ fast and predictable, and also makes it easy to keep a pointer to something already destroyed.

In this lesson: A memory map with stack frames, heap blocks and object layouts. Step through constructors and destructors. shared_ptr shows its control block and move visibly steals the pointer. Break-it: a held iterator dangles after push_back.