@@ -81,7 +81,7 @@ Rustはデフォルトで「スタックアロケート」、すなわち基本
81
81
Well, when a function gets called, some memory gets allocated for all of its
82
82
local variables and some other information. This is called a ‘stack frame’, and
83
83
for the purpose of this tutorial, we’re going to ignore the extra information
84
- and just consider the local variables we’re allocating. So in this case, when
84
+ and only consider the local variables we’re allocating. So in this case, when
85
85
`main()` is run, we’ll allocate a single 32-bit integer for our stack frame.
86
86
This is automatically handled for you, as you can see; we didn’t have to write
87
87
any special Rust code or anything.
@@ -226,57 +226,62 @@ Let’s try a three-deep example:
226
226
3段階の深さの例を見てみましょう:
227
227
228
228
``` rust
229
- fn bar () {
229
+ fn italic () {
230
230
let i = 6 ;
231
231
}
232
232
233
- fn foo () {
233
+ fn bold () {
234
234
let a = 5 ;
235
235
let b = 100 ;
236
236
let c = 1 ;
237
237
238
- bar ();
238
+ italic ();
239
239
}
240
240
241
241
fn main () {
242
242
let x = 42 ;
243
243
244
- foo ();
244
+ bold ();
245
245
}
246
246
```
247
247
248
+ <!--
249
+ We have some kooky function names to make the diagrams clearer.
250
+ -->
251
+ 分かりやすいようにちょと変な名前をつけています。
252
+
248
253
<!--
249
254
Okay, first, we call `main()`:
250
255
-->
251
- いいですか 、まず、 ` main() ` を呼び出します:
256
+ それでは 、まず、 ` main() ` を呼び出します:
252
257
253
258
| Address | Name | Value |
254
259
| ---------| ------| -------|
255
260
| 0 | x | 42 |
256
261
257
262
<!--
258
- Next up, `main()` calls `foo ()`:
263
+ Next up, `main()` calls `bold ()`:
259
264
-->
260
- 次に、 ` main() ` は ` foo ()` を呼び出します:
265
+ 次に、 ` main() ` は ` bold ()` を呼び出します:
261
266
262
267
| Address | Name | Value |
263
268
| ---------| ------| -------|
264
- | 3 | c | 1 |
265
- | 2 | b | 100 |
266
- | 1 | a | 5 |
269
+ | ** 3 ** | ** c ** | ** 1 ** |
270
+ | ** 2 ** | ** b ** | ** 100** |
271
+ | ** 1 ** | ** a ** | ** 5 ** |
267
272
| 0 | x | 42 |
268
273
269
274
<!--
270
- And then `foo ()` calls `bar ()`:
275
+ And then `bold ()` calls `italic ()`:
271
276
-->
272
- そして ` foo ()` は ` bar ()` を呼び出します:
277
+ そして ` bold ()` は ` italic ()` を呼び出します:
273
278
274
279
| Address | Name | Value |
275
280
| ---------| ------| -------|
276
- | 4 | i | 6 |
277
- | 3 | c | 1 |
278
- | 2 | b | 100 |
279
- | 1 | a | 5 |
281
+ | * 4 * | * i * | * 6 * |
282
+ | ** 3 ** | ** c ** | ** 1 ** |
283
+ | ** 2 ** | ** b ** | ** 100** |
284
+ | ** 1 ** | ** a ** | ** 5 ** |
280
285
| 0 | x | 42 |
281
286
282
287
<!--
@@ -285,22 +290,22 @@ Whew! Our stack is growing tall.
285
290
ふう、スタックが高く伸びましたね。
286
291
287
292
<!--
288
- After `bar ()` is over, its frame is deallocated, leaving just `foo ()` and
293
+ After `italic ()` is over, its frame is deallocated, leaving just `bold ()` and
289
294
`main()`:
290
295
-->
291
- ` bar ()` が終了した後、そのフレームはデアロケートされて ` foo ()` と ` main() ` だけが残ります:
296
+ ` italic ()` が終了した後、そのフレームはデアロケートされて ` bold ()` と ` main() ` だけが残ります:
292
297
293
298
| Address | Name | Value |
294
299
| ---------| ------| -------|
295
- | 3 | c | 1 |
296
- | 2 | b | 100 |
297
- | 1 | a | 5 |
300
+ | ** 3 ** | ** c ** | ** 1 ** |
301
+ | ** 2 ** | ** b ** | ** 100** |
302
+ | ** 1 ** | ** a ** | ** 5 ** |
298
303
| 0 | x | 42 |
299
304
300
305
<!--
301
- And then `foo ()` ends, leaving just `main()`:
306
+ And then `bold ()` ends, leaving just `main()`:
302
307
-->
303
- そして ` foo ()` が終了すると ` main() ` だけが残ります:
308
+ そして ` bold ()` が終了すると ` main() ` だけが残ります:
304
309
305
310
| Address | Name | Value |
306
311
| ---------| ------| -------|
@@ -392,7 +397,7 @@ location we’ve asked for.
392
397
We haven’t really talked too much about what it actually means to allocate and
393
398
deallocate memory in these contexts. Getting into very deep detail is out of
394
399
the scope of this tutorial, but what’s important to point out here is that
395
- the heap isn’t just a stack that grows from the opposite end. We’ll have an
400
+ the heap isn’t a stack that grows from the opposite end. We’ll have an
396
401
example of this later in the book, but because the heap can be allocated and
397
402
freed in any order, it can end up with ‘holes’. Here’s a diagram of the memory
398
403
layout of a program which has been running for a while now:
@@ -518,7 +523,7 @@ What about when we call `foo()`, passing `y` as an argument?
518
523
| 0 | x | 5 |
519
524
520
525
<!--
521
- Stack frames aren’t just for local bindings, they’re for arguments too. So in
526
+ Stack frames aren’t only for local bindings, they’re for arguments too. So in
522
527
this case, we need to have both `i`, our argument, and `z`, our local variable
523
528
binding. `i` is a copy of the argument, `y`. Since `y`’s value is `0`, so is
524
529
`i`’s.
@@ -530,11 +535,11 @@ binding. `i` is a copy of the argument, `y`. Since `y`’s value is `0`, so is
530
535
531
536
<!--
532
537
This is one reason why borrowing a variable doesn’t deallocate any memory: the
533
- value of a reference is just a pointer to a memory location. If we got rid of
538
+ value of a reference is a pointer to a memory location. If we got rid of
534
539
the underlying memory, things wouldn’t work very well.
535
540
-->
536
541
これは、変数を借用してもどのメモリもデアロケートされることがないことのひとつの理由になっています。
537
- つまり、参照の値はメモリ上の位置を示す単なるポインタです 。
542
+ つまり、参照の値はメモリ上の位置を示すポインタです 。
538
543
もしポインタが指しているメモリを取り去ってしまうと、ことが立ちゆかなくなってしまうでしょう。
539
544
540
545
# 複雑な例
@@ -681,11 +686,11 @@ Next, `foo()` calls `bar()` with `x` and `z`:
681
686
682
687
<!--
683
688
We end up allocating another value on the heap, and so we have to subtract one
684
- from (2<sup>30</sup>) - 1. It’s easier to just write that than `1,073,741,822`. In any
689
+ from (2<sup>30</sup>) - 1. It’s easier to write that than `1,073,741,822`. In any
685
690
case, we set up the variables as usual.
686
691
-->
687
692
その結果、ヒープに値をもうひとつアロケートすることになるので、(2<sup >30</sup >) - 1から1を引かなくてはなりません。
688
- そうすることは、単に ` 1,073,741,822 ` と書くよりは簡単です。
693
+ そうすることは、 ` 1,073,741,822 ` と書くよりは簡単です。
689
694
いずれにせよ、いつものように変数を準備します。
690
695
691
696
<!--
@@ -800,13 +805,13 @@ instead.
800
805
801
806
<!--
802
807
So if the stack is faster and easier to manage, why do we need the heap? A big
803
- reason is that Stack-allocation alone means you only have LIFO semantics for
808
+ reason is that Stack-allocation alone means you only have 'Last In First Out ( LIFO)' semantics for
804
809
reclaiming storage. Heap-allocation is strictly more general, allowing storage
805
810
to be taken from and returned to the pool in arbitrary order, but at a
806
811
complexity cost.
807
812
-->
808
813
スタックのほうが速くて管理しやすいというのであれば、なぜヒープが要るのでしょうか?
809
- 大きな理由のひとつは、スタックアロケーションだけしかないということはストレージの再利用にLIFOセマンティクスをとるしかないということだからです 。
814
+ 大きな理由のひとつは、スタックアロケーションだけしかないということはストレージの再利用に「Last In First Out(LIFO)(訳注: 後入れ先出し)」セマンティクスをとるしかないということだからです 。
810
815
ヒープアロケーションは厳密により普遍的で、ストレージを任意の順番でプールから取得したり、プールに返却することが許されているのですが、よりコストがかさみます。
811
816
812
817
<!--
@@ -822,15 +827,15 @@ has two big impacts: runtime efficiency and semantic impact.
822
827
<!-- ## Runtime Efficiency-->
823
828
824
829
<!--
825
- Managing the memory for the stack is trivial: The machine just
830
+ Managing the memory for the stack is trivial: The machine
826
831
increments or decrements a single value, the so-called “stack pointer”.
827
832
Managing memory for the heap is non-trivial: heap-allocated memory is freed at
828
833
arbitrary points, and each block of heap-allocated memory can be of arbitrary
829
- size, the memory manager must generally work much harder to identify memory for
830
- reuse.
834
+ size, so the memory manager must generally work much harder to
835
+ identify memory for reuse.
831
836
-->
832
- スタックのメモリを管理するのは些細なことです: 機械は「スタックポインタ」と呼ばれる単一の値を増減するだけです 。
833
- ヒープのメモリを管理するのは些細なことではありません: ヒープアロケートされたメモリは任意の時点で解放され、またヒープアロケートされたそれぞれのブロックは任意のサイズになりうるので、一般的にメモリマネージャは再利用するメモリを特定するためにより多く働きます 。
837
+ スタックのメモリを管理するのは些細なことです: 機械は「スタックポインタ」と呼ばれる単一の値を増減します 。
838
+ ヒープのメモリを管理するのは些細なことではありません: ヒープアロケートされたメモリは任意の時点で解放され、またヒープアロケートされたそれぞれのブロックは任意のサイズになりうるので、一般的にメモリマネージャは再利用するメモリを特定するためにより多くの仕事をします 。
834
839
835
840
<!--
836
841
If you’d like to dive into this topic in greater detail, [this paper][wilson]
0 commit comments