Memory

Every variable introduced in programming fundamentals actually lives somewhere specific inside the RAM the first topic in this series described, and that specific location is called an address — a plain number, often written in hexadecimal exactly as covered in data representation, that identifies one particular spot in memory. A pointer (sometimes called a reference) is a variable that holds an address instead of holding a value directly, which is the real, physical mechanism underneath the relations covered in discrete mathematics: when the filesystem tree's "contains" edges connect a folder to the files inside it, a pointer stored in memory is literally what makes that connection, one address pointing at the location of the next item. The stack is the region of memory used automatically for function calls: every time `calculate_total_size` is called, a new stack frame is created to hold its local variables like `total` and the current loop entry, and when the function returns, that entire frame is thrown away automatically, in the exact reverse order the frames were created — fast, predictable, and requiring no manual cleanup at all. The heap is different: it's memory that has to persist beyond any single function call, so it's requested and released explicitly rather than being cleaned up automatically, which is where something like a list of file entries would actually live if a program needed that list to still exist after the function that built it had already returned. Object lifetime is the span of time a piece of memory remains valid, and the stack and heap handle this in opposite ways: a stack variable's lifetime ends automatically the instant its function returns, while a heap object's lifetime lasts until something explicitly releases it — and getting that second case wrong produces two of the most common bugs in all of programming, exactly the kind the previous topic's tracing and assertions are built to catch: a memory leak happens when heap memory is never released even though nothing needs it anymore, slowly wasting more and more RAM, while a dangling pointer happens when memory is released but something still holds its old address and tries to use it anyway, reading or writing to a location that may now hold something completely different. None of this complexity exists for ordinary local variables, which is exactly why the stack's automatic, predictable lifetime is preferred whenever it's sufficient, and the heap, with all its manual bookkeeping, is reserved for the specific cases — like data that needs to outlive the function that created it — where the stack genuinely cannot do the job.

calculate_total_size is called
a new stack frame is created automatically
Local variables live on the stack
total and the current loop entry, both automatic
Longer-lived data lives on the heap
requested and released explicitly, not automatically
Pointers connect one address to another
the real mechanism behind every relation from discrete mathematics
The function returns
its stack frame is destroyed automatically, in reverse order
A Pointer Holds an Address, Not a Value RAM is just a long row of numbered, addressable slots RAM, numbered slots 0x1000 "report.pdf" 0x1008 40 (size) 0x1010 next entry pointer variable value: 0x1000 the pointer itself is just a small number: an address following it means reading whatever is stored at that address this is exactly how one filesystem entry links to the next
Stack: Automatic — Heap: Manual Two opposite answers to the same question: when does this memory go away Stack total, loop entry created when the function is called destroyed automatically the instant it returns fast, predictable, no manual cleanup Heap a list of file entries, needed after the function returns released only when something explicitly says to release it flexible, but manual bookkeeping get heap lifetime wrong, two classic bugs appear: memory leak: never released, quietly wastes more and more RAM dangling pointer: released, but something still tries to use it

Take memory further

Ask the copilot about this term, or have our engineers assess it against your process.