Watch code run
Step through real programs one line at a time and see the stack, the heap and every pointer.
- Variables change over time: Watch three variables get created and updated, one line at a time.
- A for loop adding up a total: See the loop counter and the running total change on every pass.
- Function calls and the stack: Every call gets its own stack frame with its own copies of the arguments.
- Recursion stacks up frames: factorial(4) calls itself until it reaches the base case, then the frames unwind.
- An array is a row of boxes: Index into an array and watch a loop fill it.
- Passing an array to a function: The function gets a pointer to the caller's array, so its changes stick.
- A string ends at the null terminator: Walk a pointer along a string until it hits '\0'.
- A pointer holds an address: Make a pointer to a variable, then change the variable through it.
- Swapping through pointers: swap() receives the addresses of a and b and changes them in main's frame.
- Pointer arithmetic walks an array: p + 1 moves one element forward, not one byte.
- A pointer to a pointer: Let a function change which thing the caller's pointer points at.
- Reversing with two pointers: One pointer from each end swaps its way to the middle.
- An array on the heap: malloc gives you a block on the heap; free gives it back.
- Growing a buffer with realloc: When the block is full, realloc makes a bigger one and copies the old values over.
- Returning heap memory from a function: A heap block outlives the function that made it; the caller must free it.
- Structs and the arrow operator: A struct groups fields; a pointer to it uses -> to reach them.
- Building a linked list: Each push makes a heap node that points at the old head.
- Reversing a linked list in place: Three pointers flip every next arrow without allocating anything.
- Calling through a function pointer: A variable can hold which function to call.
- References are another name: A reference parameter works on the caller's variable, with no copy.
- An object and its member functions: The constructor sets the fields; member functions act on this object.
- Constructors, destructors and scope: Objects are destroyed automatically, in reverse order, when their scope ends.
- How a vector grows: When size reaches capacity, the vector moves everything into a bigger heap block.
- unique_ptr owns and moves: Exactly one unique_ptr owns the heap object; moving hands it over.
- shared_ptr counts its owners: Several shared_ptrs point at one object; the last one out deletes it.
- Moving instead of copying: A copy duplicates the heap buffer; a move steals it.
- Virtual functions pick the real type: A base-class pointer calls the derived class's override.
- Binary search halves the range: lo and hi close in on the target, halving the search range every pass.
- Insertion sort, step by step: Each new element slides left until it's in place.
- Inserting into a binary search tree: Each value walks down left or right and becomes a new leaf.