スタックとヒープをシンプルに理解する
値が格納される場所と、それが重要な理由を学びます
「スタックとヒープをシンプルに理解する」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Places for Data
Every value in your program lives somewhere in memory. Mojo uses two regions: the stack and the heap. 🧠
The Stack
The stack is a fast, orderly pile of memory. Local variables go here, and they vanish automatically when a function returns.
Why the Stack Is Fast
The stack is fast because its size is known and it grows and shrinks in a simple last-in, first-out order, like a stack of plates.
The Heap
The heap is a larger, flexible pool of memory. You use it when a value's size is not known until the program is actually running.
When You Need the Heap
A list that grows, a long string, or anything sized at runtime lives on the heap, since the stack needs fixed, known sizes.
Small, Fixed Values
A simple Int or Float has a fixed, tiny size, so Mojo can keep it right on the stack for the fastest possible access.
var count: Int = 7Growing Data
A List can grow without limit, so its elements live on the heap while a small handle to them sits on the stack.
var nums = List[Int](1, 2, 3)Speed Trade-Off
The heap is more flexible but slower to manage than the stack. Reaching for heap memory has a real cost you should respect.
Automatic Cleanup
Stack values clean themselves up at the end of their scope. Mojo also tracks heap values so they are freed at the right time for you.
You Rarely Choose by Hand
Most of the time Mojo decides stack or heap based on the type. Knowing the difference simply helps you reason about speed.
A Mental Model
Picture the stack as a quick scratchpad and the heap as deep storage. Fast and small versus large and flexible.
Quick Check
Let us see where a growable value lives.
Recap
The stack is fast and fixed-size; the heap is flexible for runtime-sized data. Knowing both helps you reason about speed. 🎯
よくある質問
「スタックとヒープをシンプルに理解する」レッスンは無料ですか?
はい。「スタックとヒープをシンプルに理解する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「スタックとヒープをシンプルに理解する」で何を学びますか?
値が格納される場所と、それが重要な理由を学びます ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「スタックとヒープをシンプルに理解する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- スタックとヒープをシンプルに理解する
- 怖くないポインター
- ヒープバッファの所有
- よくあるメモリバグの回避