Skip to content

Commit

Permalink
Rollup merge of rust-lang#30404 - Shiney:ImprovedStackHeap, r=stevekl…
Browse files Browse the repository at this point in the history
…abnik

…entation clearer

I could not use colors as suggested for rust-lang#29854 because Github doesn't support these in markdown, however this solution may be better for color-blind readers.
  • Loading branch information
Manishearth committed Dec 17, 2015
2 parents 8d87ed0 + 62fa40f commit 649953e
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/doc/book/the-stack-and-the-heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,63 +130,64 @@ on the stack is the first one you retrieve from it.
Let’s try a three-deep example:

```rust
fn bar() {
fn italic() {
let i = 6;
}

fn foo() {
fn bold() {
let a = 5;
let b = 100;
let c = 1;

bar();
italic();
}

fn main() {
let x = 42;

foo();
bold();
}
```

We have some kooky function names to make the diagrams clearer.

Okay, first, we call `main()`:

| Address | Name | Value |
|---------|------|-------|
| 0 | x | 42 |

Next up, `main()` calls `foo()`:
Next up, `main()` calls `bold()`:

| Address | Name | Value |
|---------|------|-------|
| 3 | c | 1 |
| 2 | b | 100 |
| 1 | a | 5 |
| **3** | **c**|**1** |
| **2** | **b**|**100**|
| **1** | **a**| **5** |
| 0 | x | 42 |

And then `foo()` calls `bar()`:
And then `bold()` calls `italic()`:

| Address | Name | Value |
|---------|------|-------|
| 4 | i | 6 |
| 3 | c | 1 |
| 2 | b | 100 |
| 1 | a | 5 |
| *4* | *i* | *6* |
| **3** | **c**|**1** |
| **2** | **b**|**100**|
| **1** | **a**| **5** |
| 0 | x | 42 |

Whew! Our stack is growing tall.

After `bar()` is over, its frame is deallocated, leaving just `foo()` and
After `italic()` is over, its frame is deallocated, leaving just `bold()` and
`main()`:

| Address | Name | Value |
|---------|------|-------|
| 3 | c | 1 |
| 2 | b | 100 |
| 1 | a | 5 |
| 0 | x | 42 |
| **3** | **c**|**1** |
| **2** | **b**|**100**|
| **1** | **a**| **5** |
| 0 | x | 42 |

And then `foo()` ends, leaving just `main()`:
And then `bold()` ends, leaving just `main()`:

| Address | Name | Value |
|---------|------|-------|
Expand Down Expand Up @@ -578,3 +579,4 @@ comes at the cost of either significant runtime support (e.g. in the form of a
garbage collector) or significant programmer effort (in the form of explicit
memory management calls that require verification not provided by the Rust
compiler).

0 comments on commit 649953e

Please sign in to comment.