Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Activity 17: My First Stack

Name:

Problems 1 and 2 we do together during lecture. Problems 3 and 4 are at the end. Hand this in on your way out.

How to draw a stack

  • One box per function call, stacked bottom to top, with main at the bottom
  • Inside each box, list the variables that call has: parameters first, then locals
  • Write the type next to every variable. x: i32, not just x
  • A frame holds every variable its function declares, from the moment the function is called. Write ??? for any that has not been given a value yet (this includes return values!)
  • A { } block inside a function gets a mini-frame: a dashed box stacked on top of its function's frame, popped off at the closing brace
  • To find a variable, read from the top down and stop at a solid line. (A dashed line connecting a frame and mini-frame can been seen through.)

Problem 1

fn main() {
    let a = 1;
    let b = add_one(a);
    println!("{}", b);
}

fn add_one(n: i32) -> i32 {
    let result = n + 1;
    // DRAW HERE
    result
}















Problem 2

Careful, this one has a scope inside a function.

fn main() {
    let mut total = 10;

    {
        let bonus = 5;
        let doubled = bonus * 4;
        total = total + bonus + doubled / 4;
        // DRAW HERE
    }

    println!("{}", total);
}



















After the closing brace, which of these still exist?




Problem 3

fn main() {
    let n = 3;
    let answer = outer(n);
    println!("{}", answer);
}

fn outer(x: i32) -> i32 {
    let y = inner(x * 2);
    let z = y + 1;
    z
}

fn inner(v: i32) -> i32 {
    let w = v * 2;
    // DRAW HERE
    w
}





























Problem 4

We walked through this example in Lecture 10, with a name for every value this time. Now let's draw it on the stack.

fn sum_to(n: u64) -> u64 {
    if n == 0 {
        let base = 0;
        // DRAW HERE
        return base;
    }
    let rest = sum_to(n - 1);
    let sum = n + rest;
    sum
}

fn main() {
    let total = sum_to(3);
    println!("{}", total);
}





















How many frames are on the stack at the deepest point of sum_to(1000)?




Solutions

Problem 1

┌──────────────────────────┐
│ add_one                  │
│   n: i32 = 1             │
│   result: i32 = 2        │
└──────────────────────────┘
┌──────────────────────────┐
│ main                     │
│   a: i32 = 1             │
│   b: i32 = ???           │
└──────────────────────────┘

Problem 2

┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
╎ { scope }                ╎
╎   bonus: i32 = 5         ╎
╎   doubled: i32 = 20      ╎
└╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┘
┌──────────────────────────┐
│ main                     │
│   total: i32 = 20        │
│          (was 10)        │
└──────────────────────────┘

After the closing brace, only total still exists, and it is still 20. bonus and doubled popped off with the mini-frame.

Problem 3

┌──────────────────────────┐
│ inner                    │
│   v: i32 = 6             │
│   w: i32 = 12            │
└──────────────────────────┘
┌──────────────────────────┐
│ outer                    │
│   x: i32 = 3             │
│   y: i32 = ???           │
│   z: i32 = ???           │
└──────────────────────────┘
┌──────────────────────────┐
│ main                     │
│   n: i32 = 3             │
│   answer: i32 = ???      │
└──────────────────────────┘

Problem 4

┌──────────────────────────┐
│ sum_to                   │
│   n: u64 = 0             │
│   base: u64 = 0          │
│   rest: u64 = ???        │
│   sum: u64 = ???         │
└──────────────────────────┘
┌──────────────────────────┐
│ sum_to                   │
│   n: u64 = 1             │
│   base: u64 = ???        │
│   rest: u64 = ???        │
│   sum: u64 = ???         │
└──────────────────────────┘
┌──────────────────────────┐
│ sum_to                   │
│   n: u64 = 2             │
│   base: u64 = ???        │
│   rest: u64 = ???        │
│   sum: u64 = ???         │
└──────────────────────────┘
┌──────────────────────────┐
│ sum_to                   │
│   n: u64 = 3             │
│   base: u64 = ???        │
│   rest: u64 = ???        │
│   sum: u64 = ???         │
└──────────────────────────┘
┌──────────────────────────┐
│ main                     │
│   total: u64 = ???       │
└──────────────────────────┘

Each call has its own n, and only the top one is running: the other three are each waiting on let rest = sum_to(n - 1). Since base is declared inside the if block's braces, drawing it in a dashed mini-frame on top of the n = 0 frame instead is also right.

After this, base of 0 goes into the n = 1 frame's rest, its sum is 1, that goes into the n = 2 frame's rest, and so on until total is 6.

sum_to(1000) at its deepest point has 1,002 frames: main, plus one sum_to frame for every n from 1000 down to 0, which is 1,001 of them. Each is small, but a big enough n runs the stack out of room, which is how a stack overflow happens.