: Vector: : resize()对比 std: : Vector: : reserve()

这篇文章的注释部分有一个关于使用 std::vector::reserve()std::vector::resize()的线程。

以下是原始代码:

void MyClass::my_method()
{
my_member.reserve(n_dim);
for(int k = 0 ; k < n_dim ; k++ )
my_member[k] = k ;
}

我相信要在 vector中编写元素,正确的做法是调用 std::vector::resize(),而不是 std::vector::reserve()

实际上,在 VS2010 SP1的调试版本中,下面的测试代码“崩溃”了:

#include <vector>


using namespace std;


int main()
{
vector<int> v;
v.reserve(10);
v[5] = 2;


return 0;
}

我是对的还是错的? VS2010 SP1是对的还是错的?

68567 次浏览

Yes you’re correct, Luchian just made a typo and is probably too coffee-deprived to realise his mistake.

There are two different methods for a reason:

std::vector::reserve will allocate the memory but will not resize your vector, which will have a logical size the same as it was before.

std::vector::resize will actually modify the size of your vector and will fill any space with objects in their default state. If they are ints, they will all be zero.

After reserve, in your case, you will need a lot of push_backs to write to element 5. If you don't wish to do that then in your case you should use resize.

One thing about reserve: if you then add elements with push_back, until you reach the capacity you have reserved, any existing references, iterators or pointers to data in your vector will remain valid. So if I reserve 1000 and my size is 5, the &vec[4] will remain the same until the vector has 1000 elements. After that, I can call push_back() and it will work, but the stored pointer of &vec[4] earlier may no longer be valid.

It depends on what you want to do. reserve does not add any elements to the vector; it only changes the capacity(), which guarantees that adding elements will not reallocate (and e.g. invalidate iterators). resize adds elements immediately. If you want to add elements later (insert(), push_back()), use reserve. If you want to access elements later (using [] or at()), use resize. So youre MyClass::my_method can be either:

void MyClass::my_method()
{
my_member.clear();
my_member.reserve( n_dim );
for ( int k = 0; k < n_dim; ++ k ) {
my_member.push_back( k );
}
}

or

void MyClass::my_method()
{
my_member.resize( n_dim );
for ( int k = 0; k < n_dim; ++ k ) {
my_member[k] = k;
}
}

Which one you chose is a question of taste, but the code you quote is clearly incorrect.

resize actually changes the amount of elements in the vector, new items are default constructed if the resize causes the vector to grow.

vector<int> v;
v.resize(10);
auto size = v.size();

in this case size is 10.

reserve on the other hand only requests that the internal buffer be grown to the specified size but does not change the "size" of the array, only its buffer size is changed.

vector<int> v;
v.reserve(10);
auto size = v.size();

in this case size is still 0.

So to answer your question, yes you are right, even if you reserve enough space you are still accessing uninitialized memory with the index operator. With an int thats not so bad but in the case of a vector of classes you would be accessing objects which have not been constructed.

Bounds checking of compilers set to debug mode can obviously be confused by this behavior which may be why you are experiencing the crash.

There probably should be a discussion about when both methods are called with a number that's LESS than the current size of the vector.

Calling reserve() with a number smaller than the capacity will not affect the size or the capacity.

Calling resize() with a number smaller than current size the container will be reduced to that size effectively destroying the excess elements.

To sum up resize() will free up memory whereas reserve() will not.