Dynamic memory: malloc, free, and realloc · الذاكرة الديناميكية: 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.
ذاكرة المكدس مقابل ذاكرة الكومة
- المتغيرات المحلية العادية تعيش على المكدس. تظهر عندما تبدأ الدالة وتختفي عند عودتها.
- أحيانًا تحتاج إلى ذاكرة تبقى أطول من الدالة، أو whose حجم تعرفه فقط وقت التشغيل.
- تأتي تلك الذاكرة من الكومة. تطلبها، وفي وقت لاحق تردها.
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تطلب من الكومة كتلة من البايتات وتُرجع مؤشرًا إليها.- استخدم
sizeofللحصول على حجم عنصر واحد:malloc(n * sizeof(int))يجعل مساحة لـnints. - الكتلة ملكك للاستخدام عبر المؤشر، تمامًا كمصفوفة:
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 وتسرب الذاكرة
- عندما تنتهي من ذاكرة الكومة، أعدها باستخدام
free(pointer). - إذا لم تقم أبداً بـ
freeلها، فإن البرنامج يتسرب الذاكرة — يحتفظ بمكان لم يعد يستخدمه. - قاعدة لهذه المهام: تقوم دالتك بالتخصيص وترجعه؛ يقوم المُختَبِر بإفراغه. لا تقم باستدعاء
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.
توسيع مصفوفة باستخدام realloc
realloc(a, newSize)يعيد حجم كتلة من الكومة، مع الاحتفاظ بالمحتوى القديم.- قد تحرك الكتلة، لذا استخدم دائمًا قيمته الإرجاعية:
a = realloc(a, ...). - هكذا توسع مصفوفة عنصرًا واحدًا في كل مرة، مثل قائمة تصبح أطول.
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))للمصفوفات الجديدة، واستخدم#include <stdlib.h>. - دالتك تُرجع المؤشر؛ الكاشف يحرره — لا تُطل
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) لتخصيص n عدد صحيح (مع malloc)، وضبط كل واحد على value، وإرجاع المؤشر. المتحقق يحرره. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
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) لتخصيص na + nb عدد صحيح، ونسخ كل a ثم كل b، وإرجاع المؤشر. المتحقق يحرره. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
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) لتوسيع مصفوفة الكومة a لاستيعاب عدد صحيح إضافي مع realloc، وتخزين value في النهاية، وزيادة *len بمقدار 1، وإرجاع المؤشر الجديد. المتحقق يحرره. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.