Ontological Layer: Physical Data Location
Memory & Pointers
Addressing, Lifetimes, and Dynamic Storage
The Coordinates of Data
Up to this realm, we structured data logically. Now, we confront where data physically lives. A variable is merely a human name for a physical memory address. Pointers give us direct control over these addresses, allowing us to build dynamic data structures that grow and shrink at runtime on the Heap, freeing us from the strict LIFO confines of the execution Stack. But with direct memory control comes the danger of leaks, dangling references, and fatal access violations.
The 12 Memory Nodes
Ontological Mapping: From Stack Frames to Lost Allocations
Stack vs Heap
- Stack: Automatic, LIFO, Fast
- Local variables & function frames
- Heap: Dynamic, Unstructured, Manual
- Runtime-created object lifecycles
Pointers & Addresses
- Variables exist at physical addresses
- The Address-Of operator (&x)
- A Pointer stores a memory address
- Indirect access to data
Dynamic Allocation
- Requesting memory at runtime
- malloc / calloc / realloc
- Releasing memory (free)
- Explicit ownership and lifetime
Dereferencing
- Accessing the target value (*ptr)
- Reading through a pointer
- Writing through a pointer
- Modifying memory remotely
Pointer Arithmetic
- Adding/subtracting to addresses
- Moves by sizeof(Type)
- Iterating through contiguous arrays
- ptr + 1 moves to the next element
Null Pointers
- Pointer holding 0x0 or NULL
- Indicates "pointing nowhere"
- Safe initialization state
- Dereferencing NULL causes crash (Segfault)
Pointers in Data Structures
- The mechanism behind Linked Lists
- Connecting Tree and Graph nodes
- Dynamic growth of structures
- Memory layout abstraction
Memory Leaks
- Allocated block loses its pointer
- Memory remains marked "in use"
- Program cannot reach or free it
- Gradual exhaustion of Heap space
Dangling Pointers
- Memory block is freed
- Pointer still holds the old address
- Points to unallocated/reused space
- Access causes unpredictable behavior
Double Free
- Calling free() on an already freed block
- Corrupts allocator data structures
- Severe security vulnerability
- Fatal program crash
References vs Pointers
- Reference: Implicit alias, cannot be null
- Cannot be reassigned after init (C++)
- Pointer: Explicit variable holding address
- Can be null, reassigned, arithmetic
12 — Memory Safety Laboratory
Memory is finite and fragile. A single lost pointer unravels an entire data structure. By mastering explicit allocation, strict ownership, and safe pointer manipulation, we graduate from abstract theory to writing software that safely controls the physical machine.