"TF" <fa****@coned.com> wrote in message
news:ae**************************@posting.google.c om...
I have a fixed array of vectors like:
vector<string> a_vStr[10];
Then I grow each vector item and use it later. Everything works fine
but I am curious about following potential problem.
When we grow a vector it may copy all of its elements to different
memory location to accomodate the new vector size. Is it possible that
at some point an element of fixed array e.g. a_vStr[5] gets invalid or
does not point to the vector it was pointing to at begining?
No, because the size of the vector object itself remains unchanged
during run-time. The size of the vector object itself is independant of
the number of elements it holds during run-time. Like any other class,
the size of the vector object is determined during compile time and
therefore is fixed. Modifying one vector in the a_vStr[] array will not
affect the other vectors in that array
For vector objects there are two blocks of memory needed; one to store
the vector object itself (in your example that would be a element in the
a_vStr[] array), and another to hold the elements of the vector (those
will be stored outside the a_vStr[] array). Certain operations on a
vector may cause the elements stored in the vector to be moved in
memory, however the memory location of the vector object itself will not
change. In other words iterators or pointers to elements in a vector may
become invalid if the vector changes, but the vectors in the a_vStr
array will remain valid.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl.