2026-02-06 Hamza COŞKUN

How Data Structures Are Stored in Memory (1/3): How Memory Works and What a Pointer Is

This post is the English version of my original Turkish article: "Veri Yapıları Bellekte Nasıl Tutulur? (1/3): Bellek Nasıl Çalışır? Pointer Nedir?"

If we write software, we eventually work with data structures. Arrays, linked lists, trees, hash tables, and graphs all exist as bytes in memory.
So before discussing complex structures, we should answer a basic question:

How does memory actually work?

Memory Is a Large Addressed Space

At runtime, your program sees memory as a big sequence of cells.
Each cell has an address and stores a value.

Conceptually:

  • Address 0x1000 -> some bytes
  • Address 0x1001 -> some bytes
  • Address 0x1002 -> some bytes

A variable is not magic; it is data living in one or more addresses.

Variables and Addresses

When you create a variable, you get:

  1. A value
  2. A location (address) where that value lives

For example, if x = 42, memory stores 42 somewhere.
x is the name we use in code; the machine uses the address.

Why Address Matters

High-level code often hides addresses, but data structures depend on them.

  • Arrays are mostly about contiguous memory
  • Linked lists are about storing references to another address
  • Trees and graphs are built from nodes that point to other nodes

Without understanding address relationships, these structures look abstract.

Stack and Heap (Practical View)

Most languages use at least two important regions:

  • Stack: fast, structured memory for function calls and local lifetimes
  • Heap: dynamic memory for data that must outlive a single function scope

The exact implementation differs by language/runtime, but the mental model is useful:

  • Stack: simpler lifetime, often automatic
  • Heap: flexible lifetime, usually managed manually or by GC

So, What Is a Pointer?

A pointer is a value whose meaning is:

"The address of another value."

Instead of storing data directly, a pointer stores where that data is.

That is why pointers are central in systems programming and data structures:

  • Node A can point to Node B
  • A structure can be shared without copying all bytes
  • Dynamic structures can grow and link at runtime

Pointer vs Value

Think of two styles:

  • Value semantics: store the actual data
  • Reference/pointer semantics: store where the data is

Both are useful.
Choosing incorrectly can cause expensive copies, lifetime bugs, or hard-to-track aliasing.

Simple Mental Model

Imagine a city:

  • Houses = memory cells
  • House numbers = addresses
  • Data = what is inside a house
  • Pointer = a note that says "go to house #42"

When a node stores next, it does not store the whole next node.
It stores the address of the next node.

Why This Matters for Data Structures

As soon as you move beyond simple arrays, you are modeling relationships between addresses.

In upcoming parts, we can go deeper into:

  • Contiguous vs non-contiguous layouts
  • Cache behavior and locality
  • Linked structures and traversal cost
  • Trade-offs between speed, memory usage, and mutation patterns

Closing

The first step to understanding data structures is understanding memory as addressed space.
Once that clicks, pointers become much less mysterious and structure design becomes clearer.