When we try to resize the memory allocated by malloc
using realloc
, we typically do this:
char *ptr = (char *)malloc(size_1);
ptr = (char *)realloc(ptr, size_2);
If size_2
may be larger or smaller than size_1
. If new size is larger then the old data is not lost and newly allocated bytes are uninitialized. The starting address contained by the ptr
may change if there is not sufficient memory at the old address to store all bytes consecutively. realloc
moves the contents of old block into the new block and ptr
will be pointing to the initial byte of this new block.
But, if memory is allocated using calloc
, I was not able to understand how realloc
function acts. Can someone please give me a brief overview about how realloc
works on memory allocated by calloc
?