Use realloc more correctly (realloc can return a new pointer)