1
bigvalue_t result; result.assign(left.size() + right.size(), 0); int carry = 0; for(size_t i = 0; i < left.size(); i++) { carry = 0; for(size_t j = 0; j < right.size(); j++) { int sum = result[i+j] + (left[i]*right[j]) + carry; result[i+j] = sum%10; carry = sum/10; } result[i+right.size()] = carry; } return result; 

Here I used assign to allocate size of result, and result passed back normally. When I use result.reserve(left.size()+right.size());, the function runs normally inside the both for loops. Somehow when I use print out the result.size(), it is always 0. Does reserve not allocate any space?

2
  • The last question is a non-sequitur. Yes, reserve allocates space, but it doesn't create elements in the container. Commented Apr 28, 2014 at 9:16
  • Its a bit hard to try following the code when we don't know the type of most variables. Commented Apr 28, 2014 at 9:19

3 Answers 3

3

It is specified as

void reserve(size_type n);

Effects: A directive that informs a vector of a planned change in size, so that it can manage the storage allocation accordingly. After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise. Reallocation happens at this point if and only if the current capacity is less than the argument of reserve(). If an exception is thrown other than by the move constructor of a non-CopyInsertable type, there are no effects.

Complexity: It does not change the size of the sequence and takes at most linear time in the size of the sequence.

So, yes, it allocates memory, but it doesn't create any objects within the container. To actually create as much elements in the vector as you want to have later, and being able to access them via op[] you need to call resize().

reserve() is for when you want to prevent things like the vector reallocation every now and then when doing lots of push_back()s.

Sign up to request clarification or add additional context in comments.

Comments

2

reserve allocates space, but doesn't really create anything. It is used in order to avoid reallocations.

For, example, if you intend to store 10000 elements, by push_back into a vector, you probably will make the vector to use re-allocations. If you use reserve before actually storing your elements, then the vector is prepared to accept about 10000 elements, thus he is prepared and the fill of the vector shall happen faster, than if you didn't use reserve.

resize, actually creates space. Note also, that resize will initialize your elements to their default values (so for an int, it will set every element to 0).

PS - In fact, when you say reserve(1000), then the vector will actually -may- allocate space for more than 1000 elements. If this happens and you store exactly 1000 elements, then the unused space remains unused (it is not de-allocated).

Comments

2

It is the difference between semantically increasing the size of the vector (resize/assign/push_back/etc), and physically creating more underlying memory for it to expand into (reserve).

That you see your code appear to work even with reserve is just because you're not triggering any OS memory errors (because the memory belongs to your vector), but just because you don't see any error messages or crashes doesn't mean your code is safe or correct: as far as the vector is concerned, you are writing into memory that belongs to it and not you.

If you'd used .at() instead of [] you'd have got an exception; as it is, you are simply invoking undefined behaviour.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.