33

I have some code which uses thousands of vectors each vector has only 4 entries, So I want to set the initial size of each vector to 4 so that I can optimize memory usage by not reserving unused memory.

I tried reserve method:

vector<Foo> bar;
bar.reserve(10);

but seems it expands and doesn't shrink, seems there also no constructor that creates a vector with a specified capacity.

Also 2 bonus questions:

What's the default initial capacity

Can I create a vector with a specific capacity?

mmohab
  • 2,303
  • 4
  • 27
  • 43
  • 2
    If you want it at 4, why didn't you set it at 4? – wallyk May 01 '14 at 18:44
  • How do you know that "it expands and doesn't shrink"? – 101010 May 01 '14 at 18:46
  • 3
    if you want it to shrink, use `shrink_to_fit()` – Red Alert May 01 '14 at 18:46
  • There is indeed no constructor that takes an initial capacity. I've often bemoaned that. Maybe one day. – Kerrek SB May 01 '14 at 18:51
  • 3
    Am I the only one around here who doesn't understand what's wrong with `vector bar; bar.reserve(4);`? – Alex May 01 '14 at 18:53
  • @Alex reserve will expand the current vector to capacity 4 if it was less than 4 otherwise it will not shrink the vector – mmohab May 01 '14 at 18:58
  • @RedAlert Your answer is the closest to what I need thanks; – mmohab May 01 '14 at 19:00
  • 3
    @mmohab: If that's what you need, then your question is certainly not worded to indicate it. If you want to shrink the vector's capacity, why are you talking about initializing it? – Benjamin Lindley May 01 '14 at 19:03
  • @BenjaminLindley because I know the size in advance so I have 2 options initialize it with initial capacity or shrink it later, I prefer the first approach – mmohab May 01 '14 at 19:05
  • @mmohab: The default initial capacity is unspecified, but in every stdlib I know of the default is zero. After that, if you add N, they'll allocate room for N, unless N=1, then they allocate for "several" (but I don't know what several is here) – Mooing Duck May 01 '14 at 19:08
  • Huh, apperently "several" is 1 for visual studio: http://rextester.com/INQ98845 – Mooing Duck May 01 '14 at 19:12
  • @mmohab if you initialize the capacity to 4 and you never exceed it, you don't need to shrink the vector. If you've allocated more than needed the only sure way to free the reserved capacity is to copy the vector into a new vector which you've initialized with `reserve(4)` and then delete the old vector. `shrink_to_fit` is not guaranteed to actually shrink the reserved capacity of the vector. – Alex May 01 '14 at 19:31
  • @mmohab: Say that you do prefer the first approach (set the capacity upfront, don't shrink), now imagine you do so. How would the fact that `reserve()` does not shrink affect you at all, considering that you did set the capacity to the correct value? I am getting mixed signals in the question and comments. Take the time to think what you are asking, and note that `shrink_to_fit` is a *request*, not a *command*. A perfectly valid (standard) implementation of that function is to not do anything at all. – David Rodríguez - dribeas May 01 '14 at 19:40
  • 1
    Come to think of it, the question would make a lot of sense if you consider the possibility than the initial capacity of a vector is more than 4 by default. So let me just say that although there is no guarantee, all well-known implementations of the standard library use 0 as the initial capacity and you can use `vector bar; bar.reserve(4);` safely. – Alex May 01 '14 at 19:45

2 Answers2

54

The capacity of a vector cannot be controlled by the constructors - there is no applicable overload.

The C++ standard doesn't give any guarantee about the capacity of a default-constructed vector vector<Foo> bar;. However all well-known implementations use 0 as the default capacity. This is something you can rely on, as allocating memory at that point just doesn't make sense.

So I believe the answer to your question is: just use

vector<Foo> bar;
bar.reserve(4);
Alex
  • 7,728
  • 3
  • 35
  • 62
16

each vector has only 4 entries

Then you're probably better off with a std::array<Foo, 4> instead.

And in case you need 4 elements because you want a mathematical vector, I suggest a struct:

struct Vector
{
    Foo x, y, z, w;
};
fredoverflow
  • 256,549
  • 94
  • 388
  • 662