Module 1.1 ⏱ 20 min read ⚡ Foundational Concept

Memory Architecture & References

Before diving into complex data structures like trees or graphs, you must understand how computer systems map variables, references, and physical memory slots regardless of your programming language.

1. The Hardware View: RAM Layout & Addresses

Think of your machine's Random Access Memory (RAM) as a massive matrix of sequential post office boxes. Every box has a unique hexadecimal address identifier. When a compiler or interpreter runs your code, it allocates specific slots for variables depending on their data type size.

In languages like C++, you directly manage memory addresses using pointers (`&` and `*`). In Python or Java, the language runtime abstracts these memory references using object references and garbage collection, but the underlying hardware execution model remains identical.

// C++ Direct Pointer & Memory Allocation int main() { int val = 42; // Stack variable int* ptr = &val; // Store memory address return 0; }
# Python Object References & id() function val = 42 # Object allocated in memory print(id(val)) # View the internal memory address reference
// Java Reference Handles public class Main { public static void main(String[] args) { Integer val = 42; // Reference variable pointing to heap object } }

2. Stack vs. Heap Allocation

The Stack: Operates on a Last-In, First-Out (LIFO) model. It handles static memory allocation, local scope variables, and function invocation frames. It is extremely fast because memory allocation is just moving a stack pointer up or down.

The Heap: Used for dynamic allocation where variable sizes or lifetimes are unknown at compile time. While flexible, managing the heap manually (like in C++) or via runtime garbage collection (like in Python/Java) impacts performance and introduces risks like leaks or fragmentation.

Quick Check: Test Your Intuition

Which statement best describes where local variables inside standard function scopes are allocated?