awesome-everything RU
↑ Back to the climb

Base CS from zero

Memory: build a tiny memory simulator

Crux Build a tiny byte-addressed memory simulator with read, write, and dereference, then use it to model a pointer chain and a stack-vs-heap scenario.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 200 min

You understand addresses, values, pointers, and the stack/heap split when you can build a model that obeys those rules and watch it behave exactly as the lessons predicted. Build a small memory simulator from the row-of-cells model up, then drive a pointer chain and a stack-vs-heap scenario through it.

Goal

Turn the unit’s mental model into running code: a fixed row of numbered byte cells you can read from and write to by address, a dereference operation that follows a stored address, and a demonstration that distinguishes value from address and stack lifetime from heap lifetime.

Project
0 of 7
Objective

Build a tiny memory simulator — a fixed array of numbered cells with read, write, and dereference — in any language, then use it to model a pointer chain and a stack-vs-heap lifetime scenario, proving each behaviour with printed before/after state.

Requirements
Acceptance criteria
  • read, write, and dereference behave exactly as specified, and out-of-range addresses are rejected rather than silently wrapping or crashing.
  • The overwrite demonstration shows the target cell changing and every other cell unchanged, printed before and after.
  • The pointer-chain demonstration reaches the data in two reads, and repointing cell A changes the dereference result without touching cell B.
  • The stack-vs-heap demonstration shows heap data surviving after the stack frame is released, and the released stack cells marked free.
  • A short write-up names the unit idea each scenario proves.
Senior stretch
  • Add multi-byte values: store a 32-bit integer across four consecutive cells and add read32/write32 that assemble and split the four bytes, so you see why one value spans four addresses.
  • Add a simple allocator for the heap region: an allocate(size) that finds free cells and returns their starting address, and a free(address) that releases them. Show fragmentation appearing after a few allocate/free cycles.
  • Add a stack-overflow guard: if simulated frames grow past the stack region's reserved size, raise a stack-overflow error instead of corrupting the heap region.
  • Visualise the memory as a printed grid that colour-codes free, stack, and heap cells, and animate it across the call scenario step by step.
Recap

Building the simulator makes the model concrete: an address is just an array index, a value is just the integer stored there, and dereferencing is two reads — exactly what the lessons described. Reserving a low heap range and a high stack range, then watching heap data survive a released frame, turns the stack-vs-heap distinction from a fact you read into behaviour you produced. Once the rules run in code, the whole unit stops being abstract.

Continue the climb ↑The instruction
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.