Dynamic memory: malloc, free, and realloc · Dynamic memory: malloc, free, และ realloc
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 and sizeof
mallocasks the heap for a block of bytes and returns a pointer to it.- Use
sizeofto get the size of one item:malloc(n * sizeof(int))makes room fornints. - The block is yours to use through the pointer, exactly like an array:
a[0],a[1], ...
malloc และ sizeof
mallocขอ Block Bytes จาก Heap และคืน Pointer指向它- ใช้
sizeofเพื่อรับ Size ของหนึ่ง item:malloc(n * sizeof(int))สร้างพื้นที่สำหรับnints - Block นั้นเป็นของคุณใช้ผ่าน Pointer เหมือน Array พอดี:
a[0],a[1], ...
free and memory leaks
- When you are done with heap memory, give it back with
free(pointer). - If you never
freeit, the program leaks memory — it holds space it no longer uses. - Rule for these tasks: your function allocates and returns; the checker frees it. Do not call
freeyourself.
free และ Memory leaks
- เมื่อคุณใช้ Heap memory เสร็จ ให้给它 back ด้วย
free(pointer) - หากคุณไม่เคย
free它 โปรแกรมจะ รั่วไหล หน่วยความจำ — It占有Spaceที่ไม่ใช้แล้ว - กฎสำหรับงานเหล่านี้: ฟังก์ชันของคุณ allocate และ return; ตัวตรวจสอบ free它. อย่า call
freeด้วยตัวเอง
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = malloc(3 * sizeof(int)); // room for 3 ints
a[0] = 5; a[1] = 6; a[2] = 7;
printf("%d\n", a[0] + a[1] + a[2]); // 18
free(a); // give it back
return 0;
}
Growing an array with realloc
realloc(a, newSize)resizes a heap block, keeping the old contents.- It may move the block, so always use its return value:
a = realloc(a, ...). - This is how you grow an array one item at a time, like a list that gets longer.
การขยาย Array ด้วย realloc
realloc(a, newSize)Resize Block บน Heap, เก็บเนื้อหาเก่าไว้- มันอาจย้าย Block, ดังนั้นต้องใช้ Return value เสมอ:
a = realloc(a, ...) - นี่คือวิธีคุณขยาย Array ทีละ(item)item, เหมือน List ที่ยาวขึ้น
Common mistakes
- Every
mallocneeds a matchingfree; do not use memory afterfree. - Check that
mallocdid not returnNULL.
ข้อผิดพลาดที่พบบ่อย
- ทุก
mallocต้องมีfreeที่สอดคล้องกัน; อย่าใช้หน่วยความจำหลังจากfree - ตรวจสอบว่า
mallocไม่ได้คืนค่าNULL
Now you try
- Use
malloc(n * sizeof(int))for new arrays, and#include <stdlib.h>. - Your function returns the pointer; the checker frees it — do not call
free. Do not write amain.
ลองดูเลย
- ใช้
malloc(n * sizeof(int))สำหรับสร้าง array ใหม่, และ#include <stdlib.h> - ฟังก์ชันของคุณคืนค่า pointer; ตัวตรวจสอบจะ free มัน — อย่า call
free. อย่า เขียนmain
Complete int *make_array(int n, int value) so it allocates n ints (with malloc), sets every one to value, and returns the pointer. The checker frees it. Do not write a main. · เติม int *make_array(int n, int value) Alloc n ints (ด้วย malloc) ตั้งค่าทั้งหมดเป็น value และส่ง pointer Checker จะ Free ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int *concat(const int a[], int na, const int b[], int nb) so it allocates na + nb ints, copies all of a then all of b, and returns the pointer. The checker frees it. Do not write a main. · เติม int *concat(const int a[], int na, const int b[], int nb) Alloc na + nb ints Copy ทั้งหมดของ a แล้วทั้งหมดของ b และส่ง pointer Checker จะ Free ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int *push(int *a, int *len, int value) so it grows the heap array a to hold one more int with realloc, stores value at the end, increases *len by 1, and returns the new pointer. The checker frees it. Do not write a main. · เติม int *push(int *a, int *len, int value) ขยาย heap array a เพื่อเก็บ int เพิ่มอีกหนึ่งตัวด้วย realloc เก็บ value ไว้ที่ท้าย เพิ่ม *len ขึ้น 1 และส่ง pointer ใหม่ Checker จะ Free ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่