Data structures without malloc lesson plan
The containers firmware really uses, every byte fixed at build time: ring buffers, pools, intrusive lists, bitsets, state tables and heaps. Ends with an event queue that runs a space shooter.
Objectives and materials
By the end, students can
- A ring buffer for events and a pool of bullets, with no heap.
- Lists threaded through the objects, and sixteen answers in two bytes.
- A game run by a table, and a heap that knows what is due next.
- Everything in one event queue: Mokxi Invaders.
Materials for the whole unit
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Queues and pools
A ring buffer for events and a pool of bullets, with no heap.
| Lesson | Time | What students do |
|---|---|---|
| Ring buffersA fixed-size queue that never moves its data: free-running uint8_t indices, a power-of-two mask, what full means, and button presses queued for a game tick. | 40 min |
|
| Fixed poolsEvery bullet the game will ever have, made at build time: a free list threaded through the slots, alloc and release in a few instructions, and what happens when it runs dry. | 40 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Fixed pools" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A bigger magazine".
Links and bits
Lists threaded through the objects, and sixteen answers in two bytes.
| Lesson | Time | What students do |
|---|---|---|
| Intrusive listsEnemies that carry their own next pointer: lists that cost no memory, moving a node between lists with two pointer writes, and unlinking with a pointer to a pointer. | 40 min |
|
| BitsetsSixteen invaders in one uint16_t: test, set and clear a bit, count them with Kernighan's trick, and shoot the lowest one with v & (v - 1). | 45 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Bitsets" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A full wave".
Rules and order
A game run by a table, and a heap that knows what is due next.
| Lesson | Time | What students do |
|---|---|---|
| State tablesA game's TITLE, PLAYING, PAUSED and GAMEOVER rules as one NEXT[state][event] table: transitions that are data, entry actions, and changing a rule by editing one cell. | 40 min |
|
| Priority queuesA binary min-heap of timed game events in an 8-slot array: the tree with no pointers, sift up and down, swap counts, and time comparisons that survive the wrap. | 45 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Priority queues" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Something sooner".
Boss
Everything in one event queue: Mokxi Invaders.
| Lesson | Time | What students do |
|---|---|---|
| Boss: an event queue for a gameMokxi Invaders with no malloc: a button interrupt and a heap of timers feed one ring of events, handled by a table of state functions, with a bullet pool and a bitset wave. | 55 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Boss: an event queue for a game" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.