Pointer to object in std::vector
I store and access an object in a std::vector as follows:
//vector is a member variable
//storing the pointer to the object in the vector
Will the pointer always (as long as I don't remove the object) be valid? Even if I add/remove other objects?
No. Adding objects may cause the vector to re-allocate its memory.
And here's proof:
for (int i=1; i <= 10; ++i)
std::clog << &vi.at(0) << '\n';
std::clog << std::endl;
Thanks for the quick reply!
As a side,
my_vector.data() returns the pointer also. No need for
Topic archived. No new replies allowed.