Can a pointer to a vector be invalidated by vector::reserve?

It's my understanding that a call to vector::reserve typically forces a reallocation, and invalidates all iterators, pointers, and references to objects contained in the vector. However, in this case I'm concerned about the validity of the pointer TO the vector. If a reallocation occurs, does the address of the vector itself change, and invalidate the pointer? If it does, what happens if I later use the indirection operator to call other vector methods, and how can that be avoided?

1
2
3
4
5
6
7
int bin_size = 50000;
vector<int> * temp;
for(int i = 0; i < a_num; ++i){
    temp = new vector<int>();
    temp->reserve(bin_size);
    _ptrs_to_bins.push_back(temp);
}
If a reallocation occurs, does the address of the vector itself change, and invalidate the pointer?


No, this is impossible.

A pointer to the vector itself cannot be invalidated unless you reallocate it elsewhere. Modifying a vector will never change its pointer.
Thanks for the quick response. :) I was not sure as to what exactly was going on when a reallocation event happened.
Topic archived. No new replies allowed.