Memoria dinámica: asignación de memoria del heap en ensamblador
Solicite y libere memoria en tiempo de ejecución desde ensamblador usando las interfaces del heap del sistema operativo (brk/mmap en Linux, VirtualAlloc en Windows) y malloc/free de la biblioteca de C.
Memoria dinámica: asignación de memoria del heap en ensamblador es una lección gratuita de Assembly Language & x86 Low-Level Systems Programming en CoddyKit. Esta es la lección 4 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 Assembly Language & x86 Low-Level Systems Programming, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Assembly Language & x86 Low-Level Systems Programming incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Stack vs Heap
The stack holds fixed, scope-bound data and is automatically reclaimed. The heap provides dynamic memory whose size and lifetime you control at run time.
When You Need the Heap
Use heap memory when the size is unknown at assemble time or the data must outlive the procedure that created it, e.g. growing buffers and data structures.
Linux: the brk Syscall
The brk/sbrk syscall grows the programs data segment. It is low level and mostly superseded by mmap for general allocation.
mov rax, 12 ; sys_brk
mov rdi, 0
syscallLinux: mmap
mmap maps fresh anonymous pages and is the modern way to get large heap regions directly from the kernel.
mov rax, 9 ; sys_mmap
syscallReleasing with munmap
Memory obtained from mmap is returned with munmap, telling the kernel those pages are free.
mov rax, 11 ; sys_munmap
syscallWindows: VirtualAlloc
On Windows you call VirtualAlloc to reserve and commit pages, and VirtualFree to release them.
call VirtualAllocUsing the C Library
When linking with the C runtime, it is far simpler to call malloc and free, which manage a heap on top of the OS primitives.
mov rdi, 64 ; bytes
call mallocChecking the Result
Allocation can fail. malloc returns NULL and syscalls return a negative error code, so always test the result before using the pointer.
test rax, rax
jz alloc_failedFreeing Memory
Every allocation needs a matching free. Forgetting to free leaks memory; freeing twice or using freed memory corrupts the heap.
mov rdi, rax ; pointer
call freeAlignment Considerations
Heap blocks are returned suitably aligned for any type. Manual allocators must respect alignment for SSE and atomic operations.
Common Pitfalls
Heap bugs are subtle:
- Memory leaks from missing free
- Use-after-free
- Double free
- Buffer overruns past the allocation
Quick Check
Test your understanding of heap allocation.
Recap
You learned heap allocation:
- Stack is automatic; heap is run-time managed
- Linux:
mmap/munmaporbrk - Windows:
VirtualAlloc/VirtualFree - With libc use
malloc/free; always check for NULL and free once
Preguntas frecuentes
¿La lección «Memoria dinámica: asignación de memoria del heap en ensamblador» es gratis?
Sí — el texto completo de «Memoria dinámica: asignación de memoria del heap en ensamblador» 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 Assembly Language & x86 Low-Level Systems Programming, actualiza a CoddyKit PRO. El curso de Assembly Language & x86 Low-Level Systems Programming incluye 4 lecciones en total.
¿Qué aprenderé en «Memoria dinámica: asignación de memoria del heap en ensamblador»?
Solicite y libere memoria en tiempo de ejecución desde ensamblador usando las interfaces del heap del sistema operativo (brk/mmap en Linux, VirtualAlloc en Windows) y malloc/free de la biblioteca de… Practicas Assembly Language & x86 Low-Level Systems Programming 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 Assembly Language & x86 Low-Level Systems Programming?
No se requiere experiencia previa. Assembly Language & x86 Low-Level Systems Programming 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 4 de 4.
¿Cuánto tiempo toma la lección «Memoria dinámica: asignación de memoria del heap en ensamblador»?
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 Assembly Language & x86 Low-Level Systems Programming?
Sí. Cada lección de Assembly Language & x86 Low-Level Systems Programming 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
- Conceptos de memoria virtual
- Llamadas al sistema de Linux (syscalls)
- Interacción con la API de Windows
- Memoria dinámica: asignación de memoria del heap en ensamblador