Pamięć dynamiczna: przydzielanie sterty w asemblerze
Pobieraj i zwalniaj pamięć w czasie działania programu z poziomu asemblera, korzystając z interfejsów sterty systemu operacyjnego (brk/mmap w systemie Linux, VirtualAlloc w systemie Windows) oraz malloc/free z biblioteki C.
Pamięć dynamiczna: przydzielanie sterty w asemblerze to bezpłatna lekcja Assembly Language & x86 Low-Level Systems Programming na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Assembly Language & x86 Low-Level Systems Programming, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Assembly Language & x86 Low-Level Systems Programming zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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
Często zadawane pytania
Czy lekcja „Pamięć dynamiczna: przydzielanie sterty w asemblerze” jest bezpłatna?
Tak — pełny tekst „Pamięć dynamiczna: przydzielanie sterty w asemblerze” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Assembly Language & x86 Low-Level Systems Programming, przejdź na CoddyKit PRO. Kurs Assembly Language & x86 Low-Level Systems Programming zawiera 4 lekcji w sumie.
Co nauczysz się w „Pamięć dynamiczna: przydzielanie sterty w asemblerze”?
Pobieraj i zwalniaj pamięć w czasie działania programu z poziomu asemblera, korzystając z interfejsów sterty systemu operacyjnego (brk/mmap w systemie Linux, VirtualAlloc w systemie Windows) oraz mal… Ćwiczysz Assembly Language & x86 Low-Level Systems Programming z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Assembly Language & x86 Low-Level Systems Programming?
Nie wymagamy żadnego doświadczenia. Assembly Language & x86 Low-Level Systems Programming w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Pamięć dynamiczna: przydzielanie sterty w asemblerze”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Assembly Language & x86 Low-Level Systems Programming?
Tak. Każda lekcja Assembly Language & x86 Low-Level Systems Programming zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Koncepcje pamięci wirtualnej
- Wywołania systemowe Linuksa (syscalls)
- Interakcja z Windows API
- Pamięć dynamiczna: przydzielanie sterty w asemblerze