Corrcting invalid free->malloc by using realloc