array of vector!

T

TF

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?
 
P

Peter van Merkerk

TF said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top