Lets say I do
char *a;
a = new char[4];
Now, is it possible to extend the size of the array further?
Say I want to keep any values I put inside a[0]
to a[3]
but now I want more space for a[4]
and a[5]
etc. Is that possible in C++?
At first I felt like maybe I could just make:
char* a[10];
a[0] = new char[size];
Whenever more space is needed I can go to a[1]
and allocate more space. This is the only alternative I can think of at the moment.
Instead of char* a , you can use char** a;
i.e.
Though this is a work around, I would suggest to used vector.
Unfortunately it's not possible in C++. You have to allocate a new area and copy the old into the new.
However, C++ have other facilities, like
std::vector
, that alleviates the need to manually handle these things.