I wanted to know the behavior of std::vector::reserve()
in following situations:
reserve(N)
is called multiple times one after another immediately.
Will the earlier reserve(N1)
get added up or overwritten ?reserve(N1)
gets overwritten with the latest call,
then what happens if the latest reserve(Nn)
demands less number of slots ?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
?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 ?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.
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.
1) std::vector is a sequence container that encapsulates dynamic size arrays.
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.
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...
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 tocapacity()
, this call has no effect. Otherwise, it is a request for allocation of additional memory. If the request is successful, thencapacity()
is greater than or equal ton
; otherwise,capacity()
is unchanged. In either case,size()
is unchanged.
Suppose
reserve(N)
is called multiple times one after another immediately. Will the earlierreserve(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 latestreserve(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 wepush_back()
1 more element(X+1)
, then that object would have to get relocated; but we haven't yet performedpush_back()
. What happens if we callreserve()
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 ofcapacity()
otherwise. Reallocation happens at this point if and only if the current capacity is less than the argument ofreserve()
.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 toreserve()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With