← All Lessons Lesson 1 / 68
Lesson 1

Understanding the Memory Model

Understanding the Memory Model

Before you learn about data structures like arrays, you need to know one simple thing first: where does a computer keep its data? Once you understand how a computer stores and finds information, everything about data structures becomes much easier to follow.

Why we need memory

Think about a simple task: adding two numbers.

The part of the computer that does the math is the CPU (the "brain"). Inside it, a small unit called the ALU (Arithmetic Logic Unit) does calculations. So when you ask the computer to do 10 + 6, the ALU happily gives back 16.

But here is the question: where does that 16 go? You will probably want to use it later. The CPU is great at calculating, but it is not built to hold lots of results. It needs a separate place to store the answer and get it back when needed.

That place is computer memory.

What is computer memory (RAM)?

Computer memory, also called RAM, is a chip — just like the CPU is a chip. But it has a different job:

  • The CPU *does the work* (adding, subtracting, comparing).
  • The memory (RAM) *stores the data* (the numbers, letters, and results).

A good way to picture it: the CPU is like a person solving a math problem in their head, and memory is the notebook where they jot down intermediate answers so they don't forget them.

Everything becomes 0s and 1s

Memory is an electronic chip, so it only understands electrical signals: high voltage and low voltage. We write these as:

  • 1 for high (think: switch on)
  • 0 for low (think: switch off)

This means *every* piece of data must be turned into a string of 0s and 1s before it can be stored. This form is called binary.

  • Numbers are converted to binary by writing them in base 2. For example, 10 in binary is 1010.
  • Non-numeric data (like letters) is first turned into a number using an *encoding*, and then that number is turned into binary.

So whether it's a number, a letter, or a picture — to the computer, it's all just 0s and 1s.

A simple way to picture memory: numbered boxes

When you write a program, you should not have to worry about voltages and chips. That is what a memory model is for: it gives you a simple mental picture of memory so you can think clearly about your data.

Here is the picture to keep in your head:

> Memory is a long row of numbered boxes, starting at box 0 and ending at box n - 1, where n is the total number of boxes.

This single idea covers 99% of what you need to know as a programmer.

Now let's look inside one box.

  • Each box holds 8 bits.
  • A bit is one binary digit — it is either 0 or 1, like a tiny switch that is on or off.
  • A group of 8 bits is called a byte.

So a byte is the basic unit of data, the same way a meter is a basic unit of distance. One box in our picture = one byte = 8 bits.

How do we find our data again? Addresses

Storing data is easy, but to *get it back* you need to know exactly which box it's in. So every box needs a unique label.

The simplest label is just the box's position counted from the very first box: 0, 1, 2, 3, and so on. This position number is called the address of the byte.

One important rule:

> The address of a piece of data is the address of the first byte where that data begins.

So if a number is spread across several boxes, you only remember where it *starts*. From there, the computer reads as many boxes as it needs.

How a program actually runs

Your code does not run directly. First a tool called the compiler turns your code into machine code (raw instructions made of 0s and 1s). Then:

  1. The whole machine code is loaded into memory.
  2. The CPU starts at the first address of that code.
  3. It reads one instruction, runs it, then moves to the next address — going through them in order.

While running, the CPU constantly talks to memory:

  • WRITE — the CPU sends a result to memory to be stored.
  • READ — the CPU pulls data back out of memory to use it.

This back-and-forth is like a brain: one part breaks down the problem (the CPU), and another part remembers the in-between results (the memory). Notice that memory holds both things at once — the program's instructions *and* the data the program creates or changes.

Why this matters

This simple model — numbered boxes, bytes, and addresses — is the foundation. It explains how basic data types like integers, characters, and floats are stored, and later it will explain how things like pointers and arrays work. Keep this picture in mind, and the rest of data structures will make sense.

Understanding the Memory Model
Diagram — click to zoom (scroll / drag to pan)