When I read this post: https://stackoverflow.com/a/42448319/3336423
I understand, that calling push_back in a for loop is unsafe because:
If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.
Then, I assume that if I guarantee the capacity won't be exceeded, it would be safe....
But apparently not, all implementations below will crash after first push_back
is called (using Visual Studio), even if I can see that capacity remains unchanged within the loop (so I assume the vector does not reallocate its memory):
Version1:
std::vector<int> v1{ 3, 4, 5 };
v1.reserve( v1.size()*2 );
size_t c1 = v1.capacity();
for ( auto val : v1 )
{
v1.push_back( val );
c1 = v1.capacity();
}
Version2:
std::vector<int> v2{ 3, 4, 5 };
v2.reserve( v2.size()*2 );
size_t c2 = v2.capacity();
auto curEnd = v2.end();
for ( auto iter = v2.begin(); iter != curEnd; ++iter )
{
v2.push_back( *iter );
c2 = v2.capacity();
}
Version3:
std::vector<int> v3{ 3, 4, 5 };
v3.reserve( v3.size()*2 );
size_t c3 = v3.capacity();
for ( auto iter = v3.begin(); iter != v3.end(); ++iter )
{
v3.push_back( *iter );
c3 = v3.capacity();
}
What makes those code crash?