Use realloc correctly

Quote from MALLOC(3) manpage:

The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind  of variable and **may be different** from ptr, or NULL if the request fails.