Memória dinâmica: alocação no heap em Assembly
Solicite e libere memória em tempo de execução a partir de Assembly usando as interfaces de heap do OS (brk/mmap no Linux, VirtualAlloc no Windows) e malloc/free da biblioteca C.
Memória dinâmica: alocação no heap em Assembly é uma aula grátis de Assembly Language & x86 Low-Level Systems Programming no CoddyKit. Esta é a aula 4 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 Assembly Language & x86 Low-Level Systems Programming, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Assembly Language & x86 Low-Level Systems Programming inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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
Perguntas Frequentes
A aula “Memória dinâmica: alocação no heap em Assembly” é grátis?
Sim — o texto completo de “Memória dinâmica: alocação no heap em Assembly” é 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 Assembly Language & x86 Low-Level Systems Programming, atualize para CoddyKit PRO. O curso de Assembly Language & x86 Low-Level Systems Programming inclui 4 aulas no total.
O que vou aprender em “Memória dinâmica: alocação no heap em Assembly”?
Solicite e libere memória em tempo de execução a partir de Assembly usando as interfaces de heap do OS (brk/mmap no Linux, VirtualAlloc no Windows) e malloc/free da biblioteca C. Você pratica Assembly Language & x86 Low-Level Systems Programming 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 Assembly Language & x86 Low-Level Systems Programming?
Nenhuma experiência prévia é necessária. Assembly Language & x86 Low-Level Systems Programming 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 4 de 4.
Quanto tempo leva a aula “Memória dinâmica: alocação no heap em Assembly”?
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 Assembly Language & x86 Low-Level Systems Programming?
Sim. Cada aula de Assembly Language & x86 Low-Level Systems Programming 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
- Conceitos de Memória Virtual
- Chamadas de Sistema do Linux (syscalls)
- Interação com a API do Windows
- Memória dinâmica: alocação no heap em Assembly