Take a look at realloc.