Using reserve() on vector

Im reading through C++ Primer 5th edition and i've reached chapter 9 on Sequential Containers and it mentioned something on reserve and Size and capacity, and I was wondering if you should use .reserve() on vector(or other applicable container) if you know how big the vector will minimally be.

So for example lets say I have an inventory, I know for a fact that the player can store at least 500 items in it, so should i do something like inventory.reserve(500);? Even if the player gets more than 500 items, it still has a lot of space before needing to allocate more memory to the new size past 500, but I dont know. What do you think?

I dont know if its even worth doing since modern computers are so powerful that something like reserving more memory for an inventory might not even be noticeable in performance.
Last edited on
No, if you're going to call reserve(), you should pass the largest possible size that the vector will need (obviously if the largest size is unbounded you should not call reserve()). The point of reserve() is that, if you can predict in advance with some degree of accuracy how much space you'll need, you can prevent reallocations during an initial population of the vector.

So, for example, if you know the vector will need somewhere between 1024 and 2048 elements, you should reserve 2048. If it turns out the vector only needed 1500 elements you'll waste a bit of memory, but you managed to populate it without any reallocations. Needless to say, it works even better when you know exactly how many elements you'll need.

If you reserve the smallest possible size you're guaranteeing that the vector will perform at least one reallocation.
Last edited on
Even if the player gets more than 500 items, it still has a lot of space before needing to allocate more memory to the new size past 500, but I dont know. What do you think?
Just to connect to this particular question: No, if you reserve space for 500 items, but add more than 500 items, you will definitely perform at least one reallocation (which is what helios said).

I wouldn't necessarily say that you should call reserve on the largest possible size. It really depends on the application, and performance should be measured and tested through A/B testing. For example, if you have some vector, and in 99% of cases, you know it has <1000 items, but in 1% of cases, it has <10000 items, it may be wasteful to reserve the worst-case scenario here. But if real-time performance is critical and allocating 10000 items, even if rare, would be perceived as freezing or lag that negatively affects the user, then you might have no choice but to go with the reserving the maximum amount in advance.
Last edited on
For 500 items, I'd say it's not a big deal. The time required to add an item is amortized constant, which means that inserting the first k items should take about as long as inserting the millionth k items.

Where you can run into trouble in with memory usage. Suppose vector<> grows by 50% each time. It's possible that all those previous blocks might not get filled (or not completely). Also, if you insert a million and 1 items and it grew after inserting 1 million, then it might have space for 500,000 items sitting there taking up space.

For these reasons, if you're inserting lots of items and memory is a concern, I'd reserve something around what you think will be needed and then, after you insert the items, resize it to the precise size needed.

But again, all of this should only be done if you have a known, or likely, problem.
Topic archived. No new replies allowed.