awesome-everything RU
↑ Back to the climb

Base CS from zero

Memory: diagram and trace reading

Crux Read memory diagrams and small snippets, do address arithmetic, trace a pointer dereference, and decide stack vs heap placement.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 14 min

Memory questions are really diagram questions. Read each layout or snippet, trace what the machine does cell by cell, and pick the answer that matches the model — address vs value, byte offsets, dereferencing, and where data lives.

Goal

Practise the moves you will repeat forever: read a memory diagram, compute a byte address by offset, follow a pointer one hop, and reason about whether a value sits on the stack or the heap.

Snippet 1 — a memory diagram

addr:   0    1    2    3    4    5
value: 25   30    3    0   99   12
Quiz

The cell at address 2 holds the value 3, and the program treats address 2 as a pointer. Dereference it: what value comes back?

Snippet 2 — address arithmetic

A 32-bit integer array starts at address 200.
Each element is 4 bytes (32 bits) wide.
Elements are laid out back-to-back with no gaps.
Quiz

At what byte address does element index 3 (the fourth element) begin?

Snippet 3 — stack vs heap placement

int* make_counter() {
    int local = 0;        // line A
    int* shared = malloc(sizeof(int));  // line B
    *shared = 0;
    return shared;        // returns a heap pointer
}
Quiz

After make_counter returns, which of local (line A) and the malloc'd int (line B) still safely exists, and why?

Snippet 4 — a write trace

Start:  addr 10 -> 7,  addr 11 -> 0
Op 1: write value 4 to address 11
Op 2: write value 9 to address 10
Op 3: write value 0 to address 10
Quiz

After all three writes, what values are at addresses 10 and 11?

Recap

Every problem here is solved by reading the diagram literally: a value can be an address, so dereferencing means a second read at that address; an array element’s byte address is base + index * elementSize; a value’s survival after a function returns depends on whether it sat on the stack frame (gone) or the heap (persists); and a write overwrites exactly one cell, last-write-wins, touching no other address. Trace cell by cell and the answer is forced.

Continue the climb ↑Memory: build a tiny memory simulator
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.