how does realloc know the size of original data?
void *realloc(void *ptr, size_t size);
So, if the implementation is like this:
Why don't you just look up how malloc/calloc/realloc/free is implemented in the C standard library you're using?
Or, if you don't have access to the source code, look at how it's implemented in one of the open-source C standard libraries.