@@ -80,15 +80,15 @@ This memory is kind of like a giant array: addresses start at zero and go
8080up to the final number. So here’s a diagram of our first stack frame:
8181
8282| Address | Name | Value |
83- + ---------+ ------+ -------+
83+ | ---------| ------| -------|
8484| 0 | x | 42 |
8585
8686We’ve got ` x ` located at address ` 0 ` , with the value ` 42 ` .
8787
8888When ` foo() ` is called, a new stack frame is allocated:
8989
9090| Address | Name | Value |
91- + ---------+ ------+ -------+
91+ | ---------| ------| -------|
9292| 2 | z | 100 |
9393| 1 | y | 5 |
9494| 0 | x | 42 |
@@ -107,7 +107,7 @@ value being stored.
107107After ` foo() ` is over, its frame is deallocated:
108108
109109| Address | Name | Value |
110- + ---------+ ------+ -------+
110+ | ---------| ------| -------|
111111| 0 | x | 42 |
112112
113113And then, after ` main() ` , even this last value goes away. Easy!
@@ -142,13 +142,13 @@ fn main() {
142142Okay, first, we call ` main() ` :
143143
144144| Address | Name | Value |
145- + ---------+ ------+ -------+
145+ | ---------| ------| -------|
146146| 0 | x | 42 |
147147
148148Next up, ` main() ` calls ` foo() ` :
149149
150150| Address | Name | Value |
151- + ---------+ ------+ -------+
151+ | ---------| ------| -------|
152152| 3 | c | 1 |
153153| 2 | b | 100 |
154154| 1 | a | 5 |
@@ -157,7 +157,7 @@ Next up, `main()` calls `foo()`:
157157And then ` foo() ` calls ` bar() ` :
158158
159159| Address | Name | Value |
160- + ---------+ ------+ -------+
160+ | ---------| ------| -------|
161161| 4 | i | 6 |
162162| 3 | c | 1 |
163163| 2 | b | 100 |
@@ -170,7 +170,7 @@ After `bar()` is over, its frame is deallocated, leaving just `foo()` and
170170` main() ` :
171171
172172| Address | Name | Value |
173- + ---------+ ------+ -------+
173+ | ---------| ------| -------|
174174| 3 | c | 1 |
175175| 2 | b | 100 |
176176| 1 | a | 5 |
@@ -179,7 +179,7 @@ After `bar()` is over, its frame is deallocated, leaving just `foo()` and
179179And then ` foo() ` ends, leaving just ` main() `
180180
181181| Address | Name | Value |
182- + ---------+ ------+ -------+
182+ | ---------| ------| -------|
183183| 0 | x | 42 |
184184
185185And then we’re done. Getting the hang of it? It’s like piling up dishes: you
@@ -206,7 +206,7 @@ fn main() {
206206Here’s what happens in memory when ` main() ` is called:
207207
208208| Address | Name | Value |
209- + ---------+ ------+ --------+
209+ | ---------| ------| --------|
210210| 1 | y | 42 |
211211| 0 | x | ?????? |
212212
@@ -218,7 +218,7 @@ it allocates some memory for the heap, and puts `5` there. The memory now looks
218218like this:
219219
220220| Address | Name | Value |
221- + -----------------+ ------+ ----------------+
221+ | -----------------| ------| ----------------|
222222| 2<sup >30</sup > | | 5 |
223223| ... | ... | ... |
224224| 1 | y | 42 |
@@ -243,7 +243,7 @@ layout of a program which has been running for a while now:
243243
244244
245245| Address | Name | Value |
246- + ----------------------+ ------+ ----------------------+
246+ | ----------------------| ------| ----------------------|
247247| 2<sup >30</sup > | | 5 |
248248| (2<sup >30</sup >) - 1 | | |
249249| (2<sup >30</sup >) - 2 | | |
@@ -266,13 +266,13 @@ Rust programs use [jemalloc][jemalloc] for this purpose.
266266Anyway, back to our example. Since this memory is on the heap, it can stay
267267alive longer than the function which allocates the box. In this case, however,
268268it doesn’t.[ ^ moving ] When the function is over, we need to free the stack frame
269- for ` main() ` . ` Box<T> ` , though, has a trick up its sleve : [ Drop] [ drop ] . The
269+ for ` main() ` . ` Box<T> ` , though, has a trick up its sleeve : [ Drop] [ drop ] . The
270270implementation of ` Drop ` for ` Box ` deallocates the memory that was allocated
271271when it was created. Great! So when ` x ` goes away, it first frees the memory
272272allocated on the heap:
273273
274274| Address | Name | Value |
275- + ---------+ ------+ --------+
275+ | ---------| ------| --------|
276276| 1 | y | 42 |
277277| 0 | x | ?????? |
278278
@@ -305,7 +305,7 @@ fn main() {
305305When we enter ` main() ` , memory looks like this:
306306
307307| Address | Name | Value |
308- + ---------+ ------+ -------+
308+ | ---------| ------| -------|
309309| 1 | y | 0 |
310310| 0 | x | 5 |
311311
@@ -315,7 +315,7 @@ memory location that `x` lives at, which in this case is `0`.
315315What about when we call ` foo() ` , passing ` y ` as an argument?
316316
317317| Address | Name | Value |
318- + ---------+ ------+ -------+
318+ | ---------| ------| -------|
319319| 3 | z | 42 |
320320| 2 | i | 0 |
321321| 1 | y | 0 |
@@ -367,7 +367,7 @@ fn main() {
367367First, we call ` main() ` :
368368
369369| Address | Name | Value |
370- + -----------------+ ------+ ----------------+
370+ | -----------------| ------| ----------------|
371371| 2<sup >30</sup > | | 20 |
372372| ... | ... | ... |
373373| 2 | j | 0 |
@@ -380,7 +380,7 @@ value pointing there.
380380Next, at the end of ` main() ` , ` foo() ` gets called:
381381
382382| Address | Name | Value |
383- + -----------------+ ------+ ----------------+
383+ | -----------------| ------| ----------------|
384384| 2<sup >30</sup > | | 20 |
385385| ... | ... | ... |
386386| 5 | z | 4 |
@@ -397,7 +397,7 @@ since `j` points at `h`.
397397Next, ` foo() ` calls ` baz() ` , passing ` z ` :
398398
399399| Address | Name | Value |
400- + -----------------+ ------+ ----------------+
400+ | -----------------| ------| ----------------|
401401| 2<sup >30</sup > | | 20 |
402402| ... | ... | ... |
403403| 7 | g | 100 |
@@ -413,7 +413,7 @@ We’ve allocated memory for `f` and `g`. `baz()` is very short, so when it’s
413413over, we get rid of its stack frame:
414414
415415| Address | Name | Value |
416- + -----------------+ ------+ ----------------+
416+ | -----------------| ------| ----------------|
417417| 2<sup >30</sup > | | 20 |
418418| ... | ... | ... |
419419| 5 | z | 4 |
@@ -426,7 +426,7 @@ over, we get rid of its stack frame:
426426Next, ` foo() ` calls ` bar() ` with ` x ` and ` z ` :
427427
428428| Address | Name | Value |
429- + ----------------------+ ------+ ----------------------+
429+ | ----------------------| ------| ----------------------|
430430| 2<sup >30</sup > | | 20 |
431431| (2<sup >30</sup >) - 1 | | 5 |
432432| ... | ... | ... |
@@ -449,7 +449,7 @@ case, we set up the variables as usual.
449449At the end of ` bar() ` , it calls ` baz() ` :
450450
451451| Address | Name | Value |
452- + ----------------------+ ------+ ----------------------+
452+ | ----------------------| ------| ----------------------|
453453| 2<sup >30</sup > | | 20 |
454454| (2<sup >30</sup >) - 1 | | 5 |
455455| ... | ... | ... |
473473After ` baz() ` is over, we get rid of ` f ` and ` g ` :
474474
475475| Address | Name | Value |
476- + ----------------------+ ------+ ----------------------+
476+ | ----------------------| ------| ----------------------|
477477| 2<sup >30</sup > | | 20 |
478478| (2<sup >30</sup >) - 1 | | 5 |
479479| ... | ... | ... |
@@ -493,7 +493,7 @@ Next, we return from `bar()`. `d` in this case is a `Box<T>`, so it also frees
493493what it points to: (2<sup >30</sup >) - 1.
494494
495495| Address | Name | Value |
496- + -----------------+ ------+ ----------------+
496+ | -----------------| ------| ----------------|
497497| 2<sup >30</sup > | | 20 |
498498| ... | ... | ... |
499499| 5 | z | 4 |
@@ -506,7 +506,7 @@ what it points to: (2<sup>30</sup>) - 1.
506506And after that, ` foo() ` returns:
507507
508508| Address | Name | Value |
509- + -----------------+ ------+ ----------------+
509+ | -----------------| ------| ----------------|
510510| 2<sup >30</sup > | | 20 |
511511| ... | ... | ... |
512512| 2 | j | 0 |
0 commit comments