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.
Merge pull request #22 from lfasnacht/realloc_fix

Use realloc correctly