Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of vector's reserve( ) method

I wanted to know the behavior of std::vector::reserve() in following situations:

  1. Suppose reserve(N) is called multiple times one after another immediately. Will the earlier reserve(N1) get added up or overwritten ?
  2. If the earlier reserve(N1) gets overwritten with the latest call, then what happens if the latest reserve(Nn) demands less number of slots ?
  3. After declaring vector if we have simply push_back() X elements, and then we call reserve(N). Will the already push_back() X elements counted in N ?
  4. Suppose, if the vector has some X pushed elements and now if we push_back() 1 more element (X+1), then that object would have to get relocated; but we haven't yet performed push_back(). What happens if we call reserve() now ? Will the object get relocated immediately ? If not, then how is the space reserved ?
like image 226
iammilind Avatar asked Feb 16 '12 02:02

iammilind


People also ask

What does the reserve function do?

A call to the function reserve modifies the capacity parameter of the vector and so the vector requests sufficient memory to store the specified number of elements. Here is a program to demonstrate the performance improvement that can be obtained by using reserve function.

How does the vector member function resize () differ from reserve ()?

The main difference between vector resize() and vector reserve() is that resize() is used to change the size of vector where reserve() doesn't. reserve() is only used to store at least the number of the specified elements without having to reallocate memory.

What kind of container is the STL vector?

1) std::vector is a sequence container that encapsulates dynamic size arrays.

Does vector insert reserve?

If the allocated memory capacity in the vector is large enough to contain the new elements, no additional allocations for the vector are needed. So no, then it won't reserve memory.


2 Answers

reserve() just makes sure that the vector's allocated memory is at least large enough to contain the number of items passed as its argument. Thus...

  1. Whichever the max value of all those passed will be the minimum resulting capacity effectively reserved.
  2. See #1.
  3. Yes.
  4. The vector allocates as much memory when reserve() is called as is necessary to store the number of items passed to reserve().

To quote from the actual standard:

void reserve(size_type n)

If n is less than or equal to capacity(), this call has no effect. Otherwise, it is a request for allocation of additional memory. If the request is successful, then capacity() is greater than or equal to n; otherwise, capacity() is unchanged. In either case, size() is unchanged.

like image 167
Amber Avatar answered Sep 28 '22 09:09

Amber


Suppose reserve(N) is called multiple times one after another immediately. Will the earlier reserve(N1) get added up or overwritten ?

Unlike std::string it is not possible to call reserve() for std::vector to shrink the capacity().Calling reserve() with an argument that is less than the current capacity() is a no-op. Hence the last reserve() call which increases the current capacity will hold good.

If the earlier reserve(N1) gets overwritten with the latest call, then what happens if the latest reserve(Nn) demands less number of slots ?

Calling reserve() with an argument that is less than the current capacity() is a no-op.

After declaring vector if we have simply push_back() X elements, and then we call reserve(N). Will the already push_back() X elements counted in N ?

reserve() just allocates(reserves) enough number of elements so Yes. Note that after calling reserve() only the capacity() of the vector is changed the size() remains unaffected.If you would need to create as many elements and not just reserve memory you should be using resize().

Suppose, if the vector has some X pushed elements and now if we push_back() 1 more element (X+1), then that object would have to get relocated; but we haven't yet performed push_back(). What happens if we call reserve() now ? Will the object get relocated immediately ? If not, then how is the space reserved ?

Yes, the relocation will happen but it depends. As said before, reserve() allocates enough memory to store as many elements as the argument passed to it. So if this number of elements is greater than what can be accommodated in current vector capacity(), relocation will happen.

Standard References:
C++03 23.2.4.2 vector capacity [lib.vector.capacity]

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().

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

Throws: length_error if n > max_size().248)

Notes: Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence. It is guaranteed that no reallocation takes place during insertions that happen after a call to reserve() until the time when an insertion would make the size of the vector greater than the size specified in the most recent call to reserve().

like image 24
Alok Save Avatar answered Sep 28 '22 08:09

Alok Save