Skip to content · ⁨ข้ามไปยังเนื้อหา⁩

Dynamic memory: malloc, free, and realloc · ⁨Dynamic memory: malloc, free, และ realloc⁩

English

Stack memory vs heap memory

  • Normal local variables live on the stack. They appear when a function starts and vanish when it returns.
  • Sometimes you need memory that outlives the function, or whose size you only know at run time.
  • That memory comes from the heap. You ask for it, and later you give it back.

ไทย

Stack memory vs Heap memory

  • ตัวแปร Local ปกติอาศัยอยู่บน Stack Они ปรากฏเมื่อฟังก์ชันเริ่มและหายไปเมื่อคืนค่า
  • บางครั้งคุณต้องการหน่วยความจำที่ ยืนยาวกว่า ฟังก์ชัน หรือwhose size คุณรู้เฉพาะตอน Run time
  • หน่วยความจำนั้นมาจาก Heap คุณขอ用它, และภายหลังคุณ给它 back

malloc takes a block of heap memory and returns a pointer; free gives the block back

Handout · ⁨เอกสารประกอบการเรียน⁩

Log in or create account · ⁨เข้าสู่ระบบหรือสร้างบัญชี⁩

IGCSE, A-Level & AP