Skip to content

Commit 2f70e8d

Browse files
committed
update the stack and the heap
1 parent ef0b473 commit 2f70e8d

File tree

2 files changed

+42
-192
lines changed

2 files changed

+42
-192
lines changed

1.9/ja/book/the-stack-and-the-heap.md

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Rustはデフォルトで「スタックアロケート」、すなわち基本
8181
Well, when a function gets called, some memory gets allocated for all of its
8282
local variables and some other information. This is called a ‘stack frame’, and
8383
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
8585
`main()` is run, we’ll allocate a single 32-bit integer for our stack frame.
8686
This is automatically handled for you, as you can see; we didn’t have to write
8787
any special Rust code or anything.
@@ -226,57 +226,62 @@ Let’s try a three-deep example:
226226
3段階の深さの例を見てみましょう:
227227

228228
```rust
229-
fn bar() {
229+
fn italic() {
230230
let i = 6;
231231
}
232232

233-
fn foo() {
233+
fn bold() {
234234
let a = 5;
235235
let b = 100;
236236
let c = 1;
237237

238-
bar();
238+
italic();
239239
}
240240

241241
fn main() {
242242
let x = 42;
243243

244-
foo();
244+
bold();
245245
}
246246
```
247247

248+
<!--
249+
We have some kooky function names to make the diagrams clearer.
250+
-->
251+
分かりやすいようにちょと変な名前をつけています。
252+
248253
<!--
249254
Okay, first, we call `main()`:
250255
-->
251-
いいですか、まず、 `main()` を呼び出します:
256+
それでは、まず、 `main()` を呼び出します:
252257

253258
| Address | Name | Value |
254259
|---------|------|-------|
255260
| 0 | x | 42 |
256261

257262
<!--
258-
Next up, `main()` calls `foo()`:
263+
Next up, `main()` calls `bold()`:
259264
-->
260-
次に、 `main()``foo()` を呼び出します:
265+
次に、 `main()``bold()` を呼び出します:
261266

262267
| Address | Name | Value |
263268
|---------|------|-------|
264-
| 3 | c | 1 |
265-
| 2 | b | 100 |
266-
| 1 | a | 5 |
269+
| **3** | **c**|**1** |
270+
| **2** | **b**|**100**|
271+
| **1** | **a**| **5** |
267272
| 0 | x | 42 |
268273

269274
<!--
270-
And then `foo()` calls `bar()`:
275+
And then `bold()` calls `italic()`:
271276
-->
272-
そして `foo()``bar()` を呼び出します:
277+
そして `bold()``italic()` を呼び出します:
273278

274279
| Address | Name | Value |
275280
|---------|------|-------|
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** |
280285
| 0 | x | 42 |
281286

282287
<!--
@@ -285,22 +290,22 @@ Whew! Our stack is growing tall.
285290
ふう、スタックが高く伸びましたね。
286291

287292
<!--
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
289294
`main()`:
290295
-->
291-
`bar()` が終了した後、そのフレームはデアロケートされて `foo()``main()` だけが残ります:
296+
`italic()` が終了した後、そのフレームはデアロケートされて `bold()``main()` だけが残ります:
292297

293298
| Address | Name | Value |
294299
|---------|------|-------|
295-
| 3 | c | 1 |
296-
| 2 | b | 100 |
297-
| 1 | a | 5 |
300+
| **3** | **c**|**1** |
301+
| **2** | **b**|**100**|
302+
| **1** | **a**| **5** |
298303
| 0 | x | 42 |
299304

300305
<!--
301-
And then `foo()` ends, leaving just `main()`:
306+
And then `bold()` ends, leaving just `main()`:
302307
-->
303-
そして `foo()` が終了すると `main()` だけが残ります:
308+
そして `bold()` が終了すると `main()` だけが残ります:
304309

305310
| Address | Name | Value |
306311
|---------|------|-------|
@@ -392,7 +397,7 @@ location we’ve asked for.
392397
We haven’t really talked too much about what it actually means to allocate and
393398
deallocate memory in these contexts. Getting into very deep detail is out of
394399
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
396401
example of this later in the book, but because the heap can be allocated and
397402
freed in any order, it can end up with ‘holes’. Here’s a diagram of the memory
398403
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?
518523
| 0 | x | 5 |
519524

520525
<!--
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
522527
this case, we need to have both `i`, our argument, and `z`, our local variable
523528
binding. `i` is a copy of the argument, `y`. Since `y`’s value is `0`, so is
524529
`i`’s.
@@ -530,11 +535,11 @@ binding. `i` is a copy of the argument, `y`. Since `y`’s value is `0`, so is
530535

531536
<!--
532537
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
534539
the underlying memory, things wouldn’t work very well.
535540
-->
536541
これは、変数を借用してもどのメモリもデアロケートされることがないことのひとつの理由になっています。
537-
つまり、参照の値はメモリ上の位置を示す単なるポインタです
542+
つまり、参照の値はメモリ上の位置を示すポインタです
538543
もしポインタが指しているメモリを取り去ってしまうと、ことが立ちゆかなくなってしまうでしょう。
539544

540545
# 複雑な例
@@ -681,11 +686,11 @@ Next, `foo()` calls `bar()` with `x` and `z`:
681686

682687
<!--
683688
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
685690
case, we set up the variables as usual.
686691
-->
687692
その結果、ヒープに値をもうひとつアロケートすることになるので、(2<sup>30</sup>) - 1から1を引かなくてはなりません。
688-
そうすることは、単に `1,073,741,822` と書くよりは簡単です。
693+
そうすることは、 `1,073,741,822` と書くよりは簡単です。
689694
いずれにせよ、いつものように変数を準備します。
690695

691696
<!--
@@ -800,13 +805,13 @@ instead.
800805

801806
<!--
802807
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
804809
reclaiming storage. Heap-allocation is strictly more general, allowing storage
805810
to be taken from and returned to the pool in arbitrary order, but at a
806811
complexity cost.
807812
-->
808813
スタックのほうが速くて管理しやすいというのであれば、なぜヒープが要るのでしょうか?
809-
大きな理由のひとつは、スタックアロケーションだけしかないということはストレージの再利用にLIFOセマンティクスをとるしかないということだからです
814+
大きな理由のひとつは、スタックアロケーションだけしかないということはストレージの再利用に「Last In First Out(LIFO)(訳注: 後入れ先出し)」セマンティクスをとるしかないということだからです
810815
ヒープアロケーションは厳密により普遍的で、ストレージを任意の順番でプールから取得したり、プールに返却することが許されているのですが、よりコストがかさみます。
811816

812817
<!--
@@ -822,15 +827,15 @@ has two big impacts: runtime efficiency and semantic impact.
822827
<!--## Runtime Efficiency-->
823828

824829
<!--
825-
Managing the memory for the stack is trivial: The machine just
830+
Managing the memory for the stack is trivial: The machine
826831
increments or decrements a single value, the so-called “stack pointer”.
827832
Managing memory for the heap is non-trivial: heap-allocated memory is freed at
828833
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.
831836
-->
832-
スタックのメモリを管理するのは些細なことです: 機械は「スタックポインタ」と呼ばれる単一の値を増減するだけです
833-
ヒープのメモリを管理するのは些細なことではありません: ヒープアロケートされたメモリは任意の時点で解放され、またヒープアロケートされたそれぞれのブロックは任意のサイズになりうるので、一般的にメモリマネージャは再利用するメモリを特定するためにより多く働きます
837+
スタックのメモリを管理するのは些細なことです: 機械は「スタックポインタ」と呼ばれる単一の値を増減します
838+
ヒープのメモリを管理するのは些細なことではありません: ヒープアロケートされたメモリは任意の時点で解放され、またヒープアロケートされたそれぞれのブロックは任意のサイズになりうるので、一般的にメモリマネージャは再利用するメモリを特定するためにより多くの仕事をします
834839

835840
<!--
836841
If you’d like to dive into this topic in greater detail, [this paper][wilson]

diff-1.6.0..1.9.0/src/doc/book/the-stack-and-the-heap.md

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)