C

calloc & realloc

Në shqip përdoret i njëjti emër.

ShpjegimiSQ

calloc() është si malloc, por e mbush memorien me zero. realloc() e ndryshon madhësinë e një blloku që e ke marrë më parë — p.sh. kur një listë rritet dhe s'nxë më. Mund ta zhvendosë bllokun diku tjetër, prandaj përdor pointer-in që të kthen.

EnglishEN

calloc() is like malloc, but fills the memory with zeros. realloc() changes the size of a block you got earlier — like when a list grows and doesn't fit any more. It may move the block somewhere else, so use the pointer it returns.

Si ta mendosh

realloc është si të kalosh në një apartament më të madh kur familja rritet: plaçkat i merr me vete, por adresa ndryshon.

Lexoje në anglisht

realloc is like moving to a bigger flat when the family grows: you take your stuff with you, but the address changes.

Shembull kodic

int *v = calloc(5, sizeof(int));     // 5 zero
int *tmp = realloc(v, 10 * sizeof(int));
if (tmp) v = tmp;
free(v);

Terma të lidhur