@@ -80,15 +80,15 @@ This memory is kind of like a giant array: addresses start at zero and go
80
80
up to the final number. So here’s a diagram of our first stack frame:
81
81
82
82
| Address | Name | Value |
83
- + ---------+ ------+ -------+
83
+ | ---------| ------| -------|
84
84
| 0 | x | 42 |
85
85
86
86
We’ve got ` x ` located at address ` 0 ` , with the value ` 42 ` .
87
87
88
88
When ` foo() ` is called, a new stack frame is allocated:
89
89
90
90
| Address | Name | Value |
91
- + ---------+ ------+ -------+
91
+ | ---------| ------| -------|
92
92
| 2 | z | 100 |
93
93
| 1 | y | 5 |
94
94
| 0 | x | 42 |
@@ -107,7 +107,7 @@ value being stored.
107
107
After ` foo() ` is over, its frame is deallocated:
108
108
109
109
| Address | Name | Value |
110
- + ---------+ ------+ -------+
110
+ | ---------| ------| -------|
111
111
| 0 | x | 42 |
112
112
113
113
And then, after ` main() ` , even this last value goes away. Easy!
@@ -142,13 +142,13 @@ fn main() {
142
142
Okay, first, we call ` main() ` :
143
143
144
144
| Address | Name | Value |
145
- + ---------+ ------+ -------+
145
+ | ---------| ------| -------|
146
146
| 0 | x | 42 |
147
147
148
148
Next up, ` main() ` calls ` foo() ` :
149
149
150
150
| Address | Name | Value |
151
- + ---------+ ------+ -------+
151
+ | ---------| ------| -------|
152
152
| 3 | c | 1 |
153
153
| 2 | b | 100 |
154
154
| 1 | a | 5 |
@@ -157,7 +157,7 @@ Next up, `main()` calls `foo()`:
157
157
And then ` foo() ` calls ` bar() ` :
158
158
159
159
| Address | Name | Value |
160
- + ---------+ ------+ -------+
160
+ | ---------| ------| -------|
161
161
| 4 | i | 6 |
162
162
| 3 | c | 1 |
163
163
| 2 | b | 100 |
@@ -170,7 +170,7 @@ After `bar()` is over, its frame is deallocated, leaving just `foo()` and
170
170
` main() ` :
171
171
172
172
| Address | Name | Value |
173
- + ---------+ ------+ -------+
173
+ | ---------| ------| -------|
174
174
| 3 | c | 1 |
175
175
| 2 | b | 100 |
176
176
| 1 | a | 5 |
@@ -179,7 +179,7 @@ After `bar()` is over, its frame is deallocated, leaving just `foo()` and
179
179
And then ` foo() ` ends, leaving just ` main() `
180
180
181
181
| Address | Name | Value |
182
- + ---------+ ------+ -------+
182
+ | ---------| ------| -------|
183
183
| 0 | x | 42 |
184
184
185
185
And then we’re done. Getting the hang of it? It’s like piling up dishes: you
@@ -206,7 +206,7 @@ fn main() {
206
206
Here’s what happens in memory when ` main() ` is called:
207
207
208
208
| Address | Name | Value |
209
- + ---------+ ------+ --------+
209
+ | ---------| ------| --------|
210
210
| 1 | y | 42 |
211
211
| 0 | x | ?????? |
212
212
@@ -218,7 +218,7 @@ it allocates some memory for the heap, and puts `5` there. The memory now looks
218
218
like this:
219
219
220
220
| Address | Name | Value |
221
- + -----------------+ ------+ ----------------+
221
+ | -----------------| ------| ----------------|
222
222
| 2<sup >30</sup > | | 5 |
223
223
| ... | ... | ... |
224
224
| 1 | y | 42 |
@@ -243,7 +243,7 @@ layout of a program which has been running for a while now:
243
243
244
244
245
245
| Address | Name | Value |
246
- + ----------------------+ ------+ ----------------------+
246
+ | ----------------------| ------| ----------------------|
247
247
| 2<sup >30</sup > | | 5 |
248
248
| (2<sup >30</sup >) - 1 | | |
249
249
| (2<sup >30</sup >) - 2 | | |
@@ -272,7 +272,7 @@ when it was created. Great! So when `x` goes away, it first frees the memory
272
272
allocated on the heap:
273
273
274
274
| Address | Name | Value |
275
- + ---------+ ------+ --------+
275
+ | ---------| ------| --------|
276
276
| 1 | y | 42 |
277
277
| 0 | x | ?????? |
278
278
@@ -305,7 +305,7 @@ fn main() {
305
305
When we enter ` main() ` , memory looks like this:
306
306
307
307
| Address | Name | Value |
308
- + ---------+ ------+ -------+
308
+ | ---------| ------| -------|
309
309
| 1 | y | 0 |
310
310
| 0 | x | 5 |
311
311
@@ -315,7 +315,7 @@ memory location that `x` lives at, which in this case is `0`.
315
315
What about when we call ` foo() ` , passing ` y ` as an argument?
316
316
317
317
| Address | Name | Value |
318
- + ---------+ ------+ -------+
318
+ | ---------| ------| -------|
319
319
| 3 | z | 42 |
320
320
| 2 | i | 0 |
321
321
| 1 | y | 0 |
@@ -367,7 +367,7 @@ fn main() {
367
367
First, we call ` main() ` :
368
368
369
369
| Address | Name | Value |
370
- + -----------------+ ------+ ----------------+
370
+ | -----------------| ------| ----------------|
371
371
| 2<sup >30</sup > | | 20 |
372
372
| ... | ... | ... |
373
373
| 2 | j | 0 |
@@ -380,7 +380,7 @@ value pointing there.
380
380
Next, at the end of ` main() ` , ` foo() ` gets called:
381
381
382
382
| Address | Name | Value |
383
- + -----------------+ ------+ ----------------+
383
+ | -----------------| ------| ----------------|
384
384
| 2<sup >30</sup > | | 20 |
385
385
| ... | ... | ... |
386
386
| 5 | z | 4 |
@@ -397,7 +397,7 @@ since `j` points at `h`.
397
397
Next, ` foo() ` calls ` baz() ` , passing ` z ` :
398
398
399
399
| Address | Name | Value |
400
- + -----------------+ ------+ ----------------+
400
+ | -----------------| ------| ----------------|
401
401
| 2<sup >30</sup > | | 20 |
402
402
| ... | ... | ... |
403
403
| 7 | g | 100 |
@@ -413,7 +413,7 @@ We’ve allocated memory for `f` and `g`. `baz()` is very short, so when it’s
413
413
over, we get rid of its stack frame:
414
414
415
415
| Address | Name | Value |
416
- + -----------------+ ------+ ----------------+
416
+ | -----------------| ------| ----------------|
417
417
| 2<sup >30</sup > | | 20 |
418
418
| ... | ... | ... |
419
419
| 5 | z | 4 |
@@ -426,7 +426,7 @@ over, we get rid of its stack frame:
426
426
Next, ` foo() ` calls ` bar() ` with ` x ` and ` z ` :
427
427
428
428
| Address | Name | Value |
429
- + ----------------------+ ------+ ----------------------+
429
+ | ----------------------| ------| ----------------------|
430
430
| 2<sup >30</sup > | | 20 |
431
431
| (2<sup >30</sup >) - 1 | | 5 |
432
432
| ... | ... | ... |
@@ -449,7 +449,7 @@ case, we set up the variables as usual.
449
449
At the end of ` bar() ` , it calls ` baz() ` :
450
450
451
451
| Address | Name | Value |
452
- + ----------------------+ ------+ ----------------------+
452
+ | ----------------------| ------| ----------------------|
453
453
| 2<sup >30</sup > | | 20 |
454
454
| (2<sup >30</sup >) - 1 | | 5 |
455
455
| ... | ... | ... |
473
473
After ` baz() ` is over, we get rid of ` f ` and ` g ` :
474
474
475
475
| Address | Name | Value |
476
- + ----------------------+ ------+ ----------------------+
476
+ | ----------------------| ------| ----------------------|
477
477
| 2<sup >30</sup > | | 20 |
478
478
| (2<sup >30</sup >) - 1 | | 5 |
479
479
| ... | ... | ... |
@@ -493,7 +493,7 @@ Next, we return from `bar()`. `d` in this case is a `Box<T>`, so it also frees
493
493
what it points to: (2<sup >30</sup >) - 1.
494
494
495
495
| Address | Name | Value |
496
- + -----------------+ ------+ ----------------+
496
+ | -----------------| ------| ----------------|
497
497
| 2<sup >30</sup > | | 20 |
498
498
| ... | ... | ... |
499
499
| 5 | z | 4 |
@@ -506,7 +506,7 @@ what it points to: (2<sup>30</sup>) - 1.
506
506
And after that, ` foo() ` returns:
507
507
508
508
| Address | Name | Value |
509
- + -----------------+ ------+ ----------------+
509
+ | -----------------| ------| ----------------|
510
510
| 2<sup >30</sup > | | 20 |
511
511
| ... | ... | ... |
512
512
| 2 | j | 0 |
0 commit comments