13

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

3
  • I believe it is legal, and probably do nothing. Nov 7, 2014 at 18:07
  • We can safely assume that the size (and, more relevantly, the capacity) will be zero or greater. Nov 7, 2014 at 18:14
  • @MikeSeymour Yeah, I wanted to say something else but it came out wrong. I just removed that part. :P
    – NPS
    Nov 7, 2014 at 18:45

7 Answers 7

25

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]

3
  • 1
    I assume you're quoting the C++ standard here? I think you should say that explicitly and provide the version of the standard quoted.
    – NPS
    Nov 7, 2014 at 18:39
  • 1
    Ok then but I still think you should state explicitly that you're quoting the C++ standard for other users that come across your answer later.
    – NPS
    Nov 7, 2014 at 18:48
  • @NPS: I think most who are likely to care would recognize the [vector.capacity] reference, but making the reference a bit more complete clearly doesn't hurt so I've edited. Nov 7, 2014 at 18:50
7

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.

2

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.

2

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

2
  • 2
    If the argument of reserve is less than or equal to the current capacity, reserve is a no-op. Since the capacity can't be negative, there's no way for 0 to be strictly greater. Nov 7, 2014 at 18:11
  • 2
    You have a typo: "then" instead of "than". I wanted to fix it myself but it says the edit must be at least 6 characters long... Idiocy...
    – NPS
    Nov 7, 2014 at 18:43
1

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.

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.