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
Completed
With base address 1000 and a 4-byte element, what address does scores[3] read, and how is it computed?
Heads-up The index counts elements, not bytes. You must multiply by the element size first: 1000 + 3 x 4 = 1012. Adding the raw index ignores how many bytes each element occupies.
Heads-up scores[3] is element 3, which is three elements past the base, not four. 1000 + 3 x 4 = 1012. Index 3 is the fourth element but its offset is 3 x element_size.
Heads-up The formula never uses the length: base + i x element_size touches only the base, index, and element size. The length is irrelevant to the address of element 3.
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
Completed
With base address 2000 and an 8-byte element, what address does big[2] read?
Heads-up The offset is index x element_size, not a fixed 8. With an 8-byte element, index 2 is 2 x 8 = 16 bytes past the base: 2000 + 16 = 2016. The element size scales the offset.
Heads-up The index counts elements; you must multiply by the element size. 2000 + 2 x 8 = 2016. Adding the raw index ignores that each element is 8 bytes wide.
Heads-up The element size is part of the formula. A 4-byte element gives 2000 + 2 x 4 = 2008; an 8-byte element gives 2016. Same index, different offset.
Snippet 3 — value cells vs reference cells
let nums = [10, 20, 30]; // array of numberslet users = [{ name: "A" }, { name: "B" }]; // array of objects
Quiz
Completed
What does each cell of nums hold versus each cell of users?
Heads-up An object is variable-size and can grow, so it cannot fit a fixed-size array cell. users cells hold references to objects living elsewhere; only nums holds its values directly.
Heads-up It is the reverse. A number is a small fixed-size value that fits in the cell directly. The objects are the ones that do not fit, so users holds references to them.
Heads-up Both arrays are contiguous grids of equal-size cells. The difference is only the cell contents: nums holds values, users holds equal-size references. Both keep the uniform layout.
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
Completed
Reading users[1].age, which sequence is correct?
Heads-up The cell at 1004 holds a reference (7100), not the object. You must follow that address to the object before you can read any field — that is the second hop.
Heads-up You do not know object 1 lives at 7100 until you read the reference out of the array cell. The first hop is the arithmetic into the array to fetch that address.
Heads-up A field is reached by its key, not by scanning positions. After following the reference to the object, the key age locates the field directly.
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.