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