awesome-everything RU
↑ Back to the climb

Base CS from zero

Data in memory: code and layout reading

Crux Read small snippets and memory layouts, compute element addresses with base + i x element_size, and reason about what a cell actually holds — a value or a reference.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 14 min

The memory layout is where these ideas become concrete. Read each snippet and its layout, do the address arithmetic by hand, and decide what each cell really holds before you pick an answer.

Goal

Practise the move you make every time you reason about data in memory: pin down the base address and element size, compute the target address, and tell whether a cell holds a value outright or a reference to something elsewhere.

Snippet 1 — the address formula

// scores is one contiguous block.
// base address = 1000, each element is 4 bytes.
let scores = [90, 80, 70, 60, 50];
let x = scores[3];   // which address does this read?
Quiz

With base address 1000 and a 4-byte element, what address does scores[3] read, and how is it computed?

Snippet 2 — element size changes the offset

// Same index, different element size.
// big has base address 2000, each element is 8 bytes.
let big = [1.5, 2.5, 3.5, 4.5];
let y = big[2];   // address?
Quiz

With base address 2000 and an 8-byte element, what address does big[2] read?

Snippet 3 — value cells vs reference cells

let nums  = [10, 20, 30];                 // array of numbers
let users = [{ name: "A" }, { name: "B" }]; // array of objects
Quiz

What does each cell of nums hold versus each cell of users?

Snippet 4 — two-hop access into an array of objects

// users has base address 1000; each reference cell is 4 bytes.
// user object 0 lives at 7000, object 1 at 7100, object 2 at 7200.
let users = [u0, u1, u2];
let a = users[1].age;   // how is this reached?
Quiz

Reading users[1].age, which sequence is correct?

Recap

Every layout question reduces to the same two moves. First the arithmetic: an element’s address is base + index x element_size — the index counts elements, the element size turns it into bytes, and the length never appears. Change the element size and the same index lands somewhere else. Second, ask what the cell holds: a small value sits in the cell directly, but an object does not fit, so the cell holds a reference and reaching a field is two hops — arithmetic into the contiguous array, then one follow out to the object.

Continue the climb ↑Data in memory: build a memory-layout model
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.