0Pricing
Mojo Academy · Lección

Pila frente a heap, explicado fácilmente

Dónde viven los valores y por qué importa.

Pila frente a heap, explicado fácilmente es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en 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 = 7

Growing 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. 🎯

Preguntas frecuentes

¿La lección «Pila frente a heap, explicado fácilmente» es gratis?

Sí — el texto completo de «Pila frente a heap, explicado fácilmente» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Pila frente a heap, explicado fácilmente»?

Dónde viven los valores y por qué importa. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Pila frente a heap, explicado fácilmente»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Pila frente a heap, explicado fácilmente
  2. Punteros sin miedo
  3. Ser propietario de búferes del heap
  4. Evitar errores de memoria habituales
← Volver a Mojo Academy