NPS
NPS

Reputation: 6355

Is std::vector::reserve(0); legal?

Is std::vector::reserve(0); legal and what will it do?

Upvotes: 13

Views: 6792

Answers (7)

marsh
marsh

Reputation: 2720

It is legal and will reserve no space. Though if the call is lower than its capacity the call will do nothing.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490108

There's nothing to prohibit it. The effect of reserve is:

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

Since the value of capacity() can never be less than 0 (it's unsigned), this can never have any effect; it can never cause a reallocation.


1. c++ standard, [vector.capacity]

Upvotes: 26

Federico Navarro
Federico Navarro

Reputation: 71

First of all, you should try to understand how Vector works. It is an array that reserve memory in order to use it when you need to store a new value trying to do the insert operation faster and efficient.

With std::vector::reserve() you can determine the amount of memory that you want to reserve, in your case, zero.

In case you want to add another value to your vector and the reserve space is zero, it will work with no problem at all, but the operation will be slower. It could be a problem if you want to do this for a lot of values, but probably you won't notice this if you do it just a few times.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

According to the C++ Standard

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

So there simply will not be a reallocation if the argument of reserve is equal to 0.

The function itself throws an exception only in one case

Throws: length_error if n > max_size().

Take into account that reserve( 0 ) is not equivalent to resize( 0 ). In the last case all elements of the vector will be removed.

Upvotes: 2

ravi
ravi

Reputation: 10733

void reserve (size_type n);

If n is greater than the current vector capacity, the function causes the container to reallocate its storage increasing its capacity to n (or greater).

In all other cases, the function call does not cause a reallocation and the vector capacity is not affected.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

The documentation provides a clear answer to this:

Increase the capacity of the container to a value that's greater or equal to new_cap. If new_cap is greater than the current capacity(), new storage is allocated, otherwise the method does nothing.

capacity() returns a value that cannot be negative. Hence, passing zero for new_cap always falls into the second category - i.e. when the function does nothing.

Upvotes: 1

cdhowie
cdhowie

Reputation: 168988

Yes, it is a legal no-op.

If new_cap is greater than the current capacity(), new storage is allocated, otherwise the method does nothing.

(Source, emphasis mine.)

Since capacity() will always be >= 0 (due to size_type being unsigned), passing a zero is guaranteed to do nothing.

Upvotes: 7

Related Questions