A realloc that increases the size of the block will retain the contents of the original memory block. Even if the memory block cannot be resized in placed, then the old data will be copied to the new block. For a realloc that reduces the size of the block, the old data will be truncated.
Note that your call to realloc will mean you lose your data if, for some reason the realloc fails. This is because realloc fails by returning NULL, but in that case the original block of memory is still valid but you can’t access it any more since you have overwritten the pointer will the NULL.
The standard pattern is:
newbuffer = realloc(buffer, newsize);
if (newbuffer == NULL)
{
//handle error
return ...
}
buffer = newbuffer;
Note also that the casting the return value from malloc is unnecessary in C and that sizeof(char) is, by definition, equal to 1.