c++ - Is the address of the first element of a vector fixed? -
c++ - Is the address of the first element of a vector fixed? -
for illustration if this:
vector<int> myvector; myvector.push_back(100); int * ptr = &(myvector[0]); myvector.clear(); myvector.push_back(10); will ptr still valid? or pointing garbage?
23.2.3 §4 says:
a.clear() [...] invalidates references, pointers, , iterators referring elements of a , may invalidate past-the-end iterator.
since there no such thing "un-invalidation", using ptr after clear results in undefined behavior.
on side note, parenthesis in &(myvector[0]) not needed. postfix operators have higher precedence prefix operators in c++, writing &myvector[0] fine.
c++ pointers stl vector
Comments
Post a Comment