Pilha versus heap, de forma simples
Entenda onde os valores vivem e por que isso importa.
Pilha versus heap, de forma simples é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Mojo Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Mojo Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 🎯
Perguntas Frequentes
A aula “Pilha versus heap, de forma simples” é grátis?
Sim — o texto completo de “Pilha versus heap, de forma simples” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Mojo Academy, atualize para CoddyKit PRO. O curso de Mojo Academy inclui 4 aulas no total.
O que vou aprender em “Pilha versus heap, de forma simples”?
Entenda onde os valores vivem e por que isso importa. Você pratica Mojo Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Mojo Academy?
Nenhuma experiência prévia é necessária. Mojo Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “Pilha versus heap, de forma simples”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Mojo Academy?
Sim. Cada aula de Mojo Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Pilha versus heap, de forma simples
- Ponteiros sem medo
- Possuindo buffers no heap
- Evitando erros comuns de memória