Stack und Heap einfach erklärt
Wo Werte liegen und warum das wichtig ist.
Stack und Heap einfach erklärt ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Stack und Heap einfach erklärt“ kostenlos?
Ja — der vollständige Text von „Stack und Heap einfach erklärt“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Stack und Heap einfach erklärt“?
Wo Werte liegen und warum das wichtig ist. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Stack und Heap einfach erklärt“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Stack und Heap einfach erklärt
- Keine Angst vor Zeigern
- Besitzende Heap-Puffer
- Häufige Speicherfehler vermeiden