Some explanation for being undefined here:
std::vector<int> v; v.reserve(10);
reserve only "reserves" memory, but does not actually resize the vector. Therefore when you do
v[i] = i * 2;
You write int to a place that has not yet been initialized, since the vector is 0 in size.
What happens in such cases: undefined . Compilers and library implementations are free to do what they like here.
However, it is determined that the size for the vector does not change to reserve (but to resize ), but capacity (something else).
source share