Yes, realloc is what you are looking for. Note that this will not work with new , you will need to use malloc (or, say, calloc ). In addition, sometimes it is simply impossible to expand the memory, so realloc will try to do it for you, but if it does not succeed, it will resort to allocating new memory, copying your contents to a new location and freeing up old memory.
user405725
source share