0Pricing
Assembly Language & x86 Low-Level Systems Programming · บทเรียน

หน่วยความจำแบบไดนามิก: การจัดสรรฮีพในแอสเซมบลี

ขอและคืนหน่วยความจำขณะโปรแกรมทำงานจากแอสเซมบลี โดยใช้อินเทอร์เฟซฮีพของ OS (brk/mmap บน Linux และ VirtualAlloc บน Windows) รวมถึง malloc/free ของไลบรารี C

หน่วยความจำแบบไดนามิก: การจัดสรรฮีพในแอสเซมบลี เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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
syscall

Linux: 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
syscall

Releasing with munmap

Memory obtained from mmap is returned with munmap, telling the kernel those pages are free.

mov rax, 11     ; sys_munmap
syscall

Windows: VirtualAlloc

On Windows you call VirtualAlloc to reserve and commit pages, and VirtualFree to release them.

call VirtualAlloc

Using 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 malloc

Checking 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_failed

Freeing 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 free

Alignment 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/munmap or brk
  • Windows: VirtualAlloc/VirtualFree
  • With libc use malloc/free; always check for NULL and free once

คำถามที่พบบ่อย

บทเรียน “หน่วยความจำแบบไดนามิก: การจัดสรรฮีพในแอสเซมบลี” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “หน่วยความจำแบบไดนามิก: การจัดสรรฮีพในแอสเซมบลี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Assembly Language & x86 Low-Level Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “หน่วยความจำแบบไดนามิก: การจัดสรรฮีพในแอสเซมบลี”

ขอและคืนหน่วยความจำขณะโปรแกรมทำงานจากแอสเซมบลี โดยใช้อินเทอร์เฟซฮีพของ OS (brk/mmap บน Linux และ VirtualAlloc บน Windows) รวมถึง malloc/free ของไลบรารี C คุณปฏิบัติ Assembly Language & x86 Low-Level Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนวคิดเกี่ยวกับหน่วยความจำเสมือน
  2. การเรียกระบบของ Linux (syscalls)
  3. การโต้ตอบกับ Windows API
  4. หน่วยความจำแบบไดนามิก: การจัดสรรฮีพในแอสเซมบลี
← กลับไปที่ Assembly Language & x86 Low-Level Systems Programming