Index
Description
The C library function void realloc(void \*ptr, size)
attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc.
If there isn’t enough memory space in the previous position, the function may move the memory block to a new location (whose address is returned by the function).
Syntax
-
ptr − This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated. If this is NULL, a new block is allocated and a pointer to it is returned by the function.
-
size − This is the new size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.
Return Value
- This function returns a pointer to the newly allocated memory oss: non c’è bisogno di fare il controllo per vedere s l’operazione è andata a buon fine (la C malloc Function ne ha bisogno)
Computational Cost
- Constant O(1)
- Linear O(n), if needs to reallocate the memory (worsts case)
Example
realloc:
Same example but using malloc: