11% スタックとヒープ
2+ <!-- % The Stack and the Heap -->
23
34<!--
45As a systems language, Rust operates at a low level. If you’re coming from a
@@ -81,7 +82,7 @@ Rustはデフォルトで「スタックアロケート」、すなわち基本
8182Well, when a function gets called, some memory gets allocated for all of its
8283local variables and some other information. This is called a ‘stack frame’, and
8384for 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
8586`main()` is run, we’ll allocate a single 32-bit integer for our stack frame.
8687This is automatically handled for you, as you can see; we didn’t have to write
8788any special Rust code or anything.
@@ -226,57 +227,62 @@ Let’s try a three-deep example:
2262273段階の深さの例を見てみましょう:
227228
228229``` rust
229- fn bar () {
230+ fn italic () {
230231 let i = 6 ;
231232}
232233
233- fn foo () {
234+ fn bold () {
234235 let a = 5 ;
235236 let b = 100 ;
236237 let c = 1 ;
237238
238- bar ();
239+ italic ();
239240}
240241
241242fn main () {
242243 let x = 42 ;
243244
244- foo ();
245+ bold ();
245246}
246247```
247248
249+ <!--
250+ We have some kooky function names to make the diagrams clearer.
251+ -->
252+ 分かりやすいようにちょと変な名前をつけています。
253+
248254<!--
249255Okay, first, we call `main()`:
250256-->
251- いいですか 、まず、 ` main() ` を呼び出します:
257+ それでは 、まず、 ` main() ` を呼び出します:
252258
253259| Address | Name | Value |
254260| ---------| ------| -------|
255261| 0 | x | 42 |
256262
257263<!--
258- Next up, `main()` calls `foo ()`:
264+ Next up, `main()` calls `bold ()`:
259265-->
260- 次に、 ` main() ` は ` foo ()` を呼び出します:
266+ 次に、 ` main() ` は ` bold ()` を呼び出します:
261267
262268| Address | Name | Value |
263269| ---------| ------| -------|
264- | 3 | c | 1 |
265- | 2 | b | 100 |
266- | 1 | a | 5 |
270+ | ** 3 ** | ** c ** | ** 1 ** |
271+ | ** 2 ** | ** b ** | ** 100** |
272+ | ** 1 ** | ** a ** | ** 5 ** |
267273| 0 | x | 42 |
268274
269275<!--
270- And then `foo ()` calls `bar ()`:
276+ And then `bold ()` calls `italic ()`:
271277-->
272- そして ` foo ()` は ` bar ()` を呼び出します:
278+ そして ` bold ()` は ` italic ()` を呼び出します:
273279
274280| Address | Name | Value |
275281| ---------| ------| -------|
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 ** |
280286| 0 | x | 42 |
281287
282288<!--
@@ -285,22 +291,22 @@ Whew! Our stack is growing tall.
285291ふう、スタックが高く伸びましたね。
286292
287293<!--
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
289295`main()`:
290296-->
291- ` bar ()` が終了した後、そのフレームはデアロケートされて ` foo ()` と ` main() ` だけが残ります:
297+ ` italic ()` が終了した後、そのフレームはデアロケートされて ` bold ()` と ` main() ` だけが残ります:
292298
293299| Address | Name | Value |
294300| ---------| ------| -------|
295- | 3 | c | 1 |
296- | 2 | b | 100 |
297- | 1 | a | 5 |
301+ | ** 3 ** | ** c ** | ** 1 ** |
302+ | ** 2 ** | ** b ** | ** 100** |
303+ | ** 1 ** | ** a ** | ** 5 ** |
298304| 0 | x | 42 |
299305
300306<!--
301- And then `foo ()` ends, leaving just `main()`:
307+ And then `bold ()` ends, leaving just `main()`:
302308-->
303- そして ` foo ()` が終了すると ` main() ` だけが残ります:
309+ そして ` bold ()` が終了すると ` main() ` だけが残ります:
304310
305311| Address | Name | Value |
306312| ---------| ------| -------|
@@ -392,7 +398,7 @@ location we’ve asked for.
392398We haven’t really talked too much about what it actually means to allocate and
393399deallocate memory in these contexts. Getting into very deep detail is out of
394400the 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
396402example of this later in the book, but because the heap can be allocated and
397403freed in any order, it can end up with ‘holes’. Here’s a diagram of the memory
398404layout 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?
518524| 0 | x | 5 |
519525
520526<!--
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
522528this case, we need to have both `i`, our argument, and `z`, our local variable
523529binding. `i` is a copy of the argument, `y`. Since `y`’s value is `0`, so is
524530`i`’s.
@@ -530,11 +536,11 @@ binding. `i` is a copy of the argument, `y`. Since `y`’s value is `0`, so is
530536
531537<!--
532538This 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
534540the underlying memory, things wouldn’t work very well.
535541-->
536542これは、変数を借用してもどのメモリもデアロケートされることがないことのひとつの理由になっています。
537- つまり、参照の値はメモリ上の位置を示す単なるポインタです 。
543+ つまり、参照の値はメモリ上の位置を示すポインタです 。
538544もしポインタが指しているメモリを取り去ってしまうと、ことが立ちゆかなくなってしまうでしょう。
539545
540546# 複雑な例
@@ -681,11 +687,11 @@ Next, `foo()` calls `bar()` with `x` and `z`:
681687
682688<!--
683689We 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
685691case, we set up the variables as usual.
686692-->
687693その結果、ヒープに値をもうひとつアロケートすることになるので、(2<sup >30</sup >) - 1から1を引かなくてはなりません。
688- そうすることは、単に ` 1,073,741,822 ` と書くよりは簡単です。
694+ そうすることは、 ` 1,073,741,822 ` と書くよりは簡単です。
689695いずれにせよ、いつものように変数を準備します。
690696
691697<!--
@@ -800,13 +806,13 @@ instead.
800806
801807<!--
802808So 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
804810reclaiming storage. Heap-allocation is strictly more general, allowing storage
805811to be taken from and returned to the pool in arbitrary order, but at a
806812complexity cost.
807813-->
808814スタックのほうが速くて管理しやすいというのであれば、なぜヒープが要るのでしょうか?
809- 大きな理由のひとつは、スタックアロケーションだけしかないということはストレージの再利用にLIFOセマンティクスをとるしかないということだからです 。
815+ 大きな理由のひとつは、スタックアロケーションだけしかないということはストレージの再利用に「Last In First Out(LIFO)(訳注: 後入れ先出し)」セマンティクスをとるしかないということだからです 。
810816ヒープアロケーションは厳密により普遍的で、ストレージを任意の順番でプールから取得したり、プールに返却することが許されているのですが、よりコストがかさみます。
811817
812818<!--
@@ -822,15 +828,15 @@ has two big impacts: runtime efficiency and semantic impact.
822828<!-- ## Runtime Efficiency-->
823829
824830<!--
825- Managing the memory for the stack is trivial: The machine just
831+ Managing the memory for the stack is trivial: The machine
826832increments or decrements a single value, the so-called “stack pointer”.
827833Managing memory for the heap is non-trivial: heap-allocated memory is freed at
828834arbitrary 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.
831837-->
832- スタックのメモリを管理するのは些細なことです: 機械は「スタックポインタ」と呼ばれる単一の値を増減するだけです 。
833- ヒープのメモリを管理するのは些細なことではありません: ヒープアロケートされたメモリは任意の時点で解放され、またヒープアロケートされたそれぞれのブロックは任意のサイズになりうるので、一般的にメモリマネージャは再利用するメモリを特定するためにより多く働きます 。
838+ スタックのメモリを管理するのは些細なことです: 機械は「スタックポインタ」と呼ばれる単一の値を増減します 。
839+ ヒープのメモリを管理するのは些細なことではありません: ヒープアロケートされたメモリは任意の時点で解放され、またヒープアロケートされたそれぞれのブロックは任意のサイズになりうるので、一般的にメモリマネージャは再利用するメモリを特定するためにより多くの仕事をします 。
834840
835841<!--
836842If you’d like to dive into this topic in greater detail, [this paper][wilson]
0 commit comments