Understanding the Memory Model
Course: DSA › Arrays › Introduction
Before diving into data structures like arrays, we need to answer a surprisingly important question: how does a computer actually store and retrieve data?
What is computer memory? Why does it exist? How does a program even run? These fundamentals are what make everything else — arrays, pointers, data structures — click into place. In this lesson, we’ll build a simple mental model of memory that works across almost all programming languages.
Memory
Let’s start with a concrete example. You ask the CPU to compute 10 + 6. It does the math and produces 16. Simple enough.
But here’s the real question: where does 16 go?
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
A[" 10 + 6 "] --> CPU["CPU<br/>ALU + Registers"]
CPU --> B[" 16 "]
The CPU can add two numbers and produce the result — but where is that result stored?
The CPU uses tiny internal slots called registers to hold values during computation. But registers are extremely limited in number. For data that needs to persist beyond a single instruction, we need something bigger: computer memory (RAM).
RAM is its own chip on your motherboard. It doesn’t compute anything — that’s the CPU’s job. Its entire purpose is to store data so it can be retrieved and updated later.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
subgraph CPU["CPU chip"]
ALU["ALU"]
Reg["Registers"]
end
subgraph RAM["RAM chip"]
C0[" "] --- C1[" "] --- C2[" "] --- C3[" "] --- C4[" "]
end
CPU -- "separate chips,\ndifferent jobs" --- RAM
CPU computes. RAM stores. They are two separate chips on your motherboard.
Everything is Binary
RAM is a chip, and chips work with electrical signals — high voltage (1) and low voltage (0). That means everything stored in memory must be represented as 0s and 1s.
Fortunately, this is easier than it sounds:
- Numbers → convert to base 2 (binary)
- Text, images, etc. → first encoded as numbers, then converted to binary
This is called the binary form of data. Every piece of information your program uses — integers, characters, floats, strings — lives in memory as a sequence of bits.
Memory trick: Think of each bit as a light switch. On = 1, Off = 0. RAM is just a huge wall of light switches.
The Memory Model
When writing real software, you don’t want to think about voltage levels and transistors. That’s where the memory model comes in.
A memory model is an abstraction — a simplified way to think about how memory works so you can reason about your code without getting lost in hardware details.
The mental model is dead simple:
Imagine memory as a long chain of numbered boxes, starting at
0and ending atn - 1, wherenis the total number of boxes. That’s it. This picture covers 99% of what you need when writing software.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
b0["0"] --- b1["1"] --- b2["2"] --- b3["3"] --- b4["4"] --- b5["5"] --- b6["6"] --- b7["n-1"]
Memory can be visualized as a linear sequence of numbered blocks.
Bits and Bytes
Each box in that chain holds exactly 8 bits. A group of 8 bits is called a byte — the basic unit of memory, like a meter is the basic unit of distance.
| Unit | What it is |
|---|---|
| Bit | A single binary digit — either 0 or 1 |
| Byte | A group of 8 bits |
So when you hear “this integer takes 4 bytes”, it means 4 consecutive boxes in memory, holding 32 bits total.
Addresses
Storing data is easy. But how do you find it again?
Each byte has a unique identifier based on its position — its address. It’s just the index of the box, counting from 0.
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
b0["0<br/>8 bits"] --- b1["1<br/>8 bits"] --- b2["2<br/>8 bits"] --- b3["3<br/>8 bits"] --- b4["4<br/>8 bits"] --- b5["5<br/>8 bits"]
addr(["Address = 3"]) --> b3
Each box is 1 byte (8 bits). Its position number is its address.
Address in memory: The address of data is the position of the first byte where that data starts.
If you store a 4-byte integer starting at address 3, its address is 3 — even though it occupies boxes 3, 4, 5, and 6.
The CPU uses these addresses to read and write data with pinpoint precision. No searching required — it jumps straight to the right box.
Analogy: Memory addresses are like house numbers on a street. You don’t walk the entire street to find a house — you go directly to the number.
Program Execution
Now let’s zoom out and see how memory fits into the bigger picture of running a program.
When you run a program:
- The compiler translates your source code into machine code
- That machine code is loaded into memory in full
- The CPU reads instructions from memory sequentially, starting at the first address
- As it executes, the CPU reads data from memory, processes it, and writes results back
---
config:
theme: base
themeVariables:
primaryColor: "#dbeafe"
primaryBorderColor: "#3b82f6"
primaryTextColor: "#1e3a5f"
lineColor: "#64748b"
secondaryColor: "#ede9fe"
tertiaryColor: "#fef9c3"
---
flowchart LR
CPU["CPU\nALU + Registers"]
subgraph MEM["Memory"]
direction TB
m0["01101010"]
m1["10100101"]
m2["00110011"]
m3["11100010"]
end
MEM -->|"READ instruction / data"| CPU
CPU -->|"WRITE result"| MEM
The CPU and memory are in constant conversation during execution.
Think of it like the human brain: one part breaks down complex problems (CPU), another part retains intermediate information (memory). They work together — neither can do much without the other.
Memory stores two kinds of things during a program’s lifetime:
- The program itself (the machine code instructions)
- The data the program creates and manipulates
Why This Matters for Arrays
This memory model is the foundation for understanding arrays — and nearly every other data structure.
- Arrays occupy a contiguous sequence of memory addresses
- Every element starts at a predictable address (calculable from the base address + element size)
- The CPU can jump to any element instantly because it knows the exact address
Once you have this mental picture — memory as a numbered line of bytes, each accessible by address — arrays become completely intuitive.
Key Takeaways
- RAM is a storage chip; the CPU is a computation chip — they’re separate and work together
- Everything in memory is stored as binary (0s and 1s)
- Memory = a long sequence of numbered bytes (each byte = 8 bits)
- Each byte has a unique address (its index from 0)
- The address of data = the first byte where it starts
- During execution, the CPU constantly reads instructions and data from memory and writes results back