In C, I would have done it using realloc.
In C++, one would normally consider using the STL vector class.
But how do I properly res
There is no good equivalent of realloc in C++. You'll need to manually duplicate the array and copy the old elements over. Fortunately, thanks to the std::copy function in , this isn't too bad:
size_t k = /* ... */
T* buffer = /* .. get old buffer of size k. .. */
T* newBuffer = new T[newSize]; // Assume newSize >= k
std::copy(buffer, buffer + k, newBuffer);
delete [] buffer;
buffer = newBuffer;
Hope this helps!
EDIT: Reordered the last two lines! Whoops!