M
Matthias Kaeppler
Dr. J.K. Becker said:Hi all,
I have vectors that holds pointers to other vectors, like so:
vector<whatever> x;
vector<whatever*> z;
Do you mean 'whatever' is a vector type?
z=&x;
That doesn't make sense.
z is a vector of pointers, not a pointer-to-vector.
Now I add something to x
x.push_back(something);
Will all the pointers in z still be valid?
That depends. If the push_back operation causes x to resize (and
reallocate storage), then probably no.
Of course I have problems with
something like this and I think not, but I didn't find anything that says
if, on using push_back, the vector allocates new memory for the whole
vector or if it just allocates new memory for the stuff that is added.
It allocates new memory as soon as its initial capacity is too small to
hold the new object.
Also, I read that if you have vectors in vectors like so:
class x
{
vector<whatever> z;
}
and then
vector<x> vec;
This is not a vector of vector but a vector of x, which has has a vector
field. That's quite a difference.
That this often causes problems with keeping track of memory therewith
causing memory leaks? If so, is there a better way to do it?
Since no pointers and dynamic resource allocation are used in your
example, the memory won't leak.