الذاكرة الديناميكية: تخصيص الذاكرة في التجميع
اطلب الذاكرة وحررها أثناء التشغيل من كود التجميع باستخدام واجهات كومة نظام التشغيل (brk/mmap في Linux وVirtualAlloc في Windows) ودالتي malloc/free في مكتبة C.
الذاكرة الديناميكية: تخصيص الذاكرة في التجميع درس مجاني في Assembly Language & x86 Low-Level Systems Programming على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Assembly Language & x86 Low-Level Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Assembly Language & x86 Low-Level Systems Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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
الأسئلة الشائعة
هل درس «الذاكرة الديناميكية: تخصيص الذاكرة في التجميع» مجاني؟
نعم — نص درس «الذاكرة الديناميكية: تخصيص الذاكرة في التجميع» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Assembly Language & x86 Low-Level Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Assembly Language & x86 Low-Level Systems Programming 4 دروس في المجموع.
ماذا ستتعلم في «الذاكرة الديناميكية: تخصيص الذاكرة في التجميع»؟
اطلب الذاكرة وحررها أثناء التشغيل من كود التجميع باستخدام واجهات كومة نظام التشغيل (brk/mmap في Linux وVirtualAlloc في Windows) ودالتي malloc/free في مكتبة C. تتمرن على Assembly Language & x86 Low-Level Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Assembly Language & x86 Low-Level Systems Programming؟
لا تُشترط خبرة سابقة. Assembly Language & x86 Low-Level Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «الذاكرة الديناميكية: تخصيص الذاكرة في التجميع»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Assembly Language & x86 Low-Level Systems Programming هذا؟
نعم. كل درس في Assembly Language & x86 Low-Level Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مفاهيم الذاكرة الافتراضية
- استدعاءات نظام Linux (syscalls)
- التفاعل مع Windows API
- الذاكرة الديناميكية: تخصيص الذاكرة في التجميع