malloc & free
Në shqip përdoret i njëjti emër.
ShpjegimiSQ
malloc() kërkon një copë memorie në heap gjatë ekzekutimit dhe të kthen një pointer drejt saj; free() e kthen kur s'të duhet më. Kontrollo gjithmonë nëse malloc ktheu NULL, dhe mos harro free — përndryshe ke memory leak.
EnglishEN
malloc() asks for a chunk of memory on the heap while the program runs and returns a pointer to it; free() gives it back when you no longer need it. Always check whether malloc returned NULL, and don't forget free — otherwise you have a memory leak.
Si ta mendosh
Si të marrësh me qira një depo: e paguan sa e përdor, dhe kur mbaron ia kthen çelësin pronarit.
Lexoje në anglisht
Like renting a storage unit: you pay while you use it, and when you're done you hand the key back.
Shembull kodic
int *notat = malloc(30 * sizeof(int));
if (notat == NULL) return 1;
notat[0] = 5;
free(notat);