size vs capacity of a vector?
Asked Answered
C

9

62

I am a bit confused about this both of these look same to me. Although it may happen that capacity and size may differ on different compilers. how it may differ. Its also said that if we are out of memory the capacity changes.

All these things are bit unclear to me.

Can somebody give an explanation.(if possible with and example or if I can do any test on any program to understand it)

Cheyney answered 9/6, 2011 at 17:35 Comment(1)
Also check out this answer #2787897. The language there is Java, but the concept is the same.Centillion
H
101

Size is not allowed to differ between multiple compilers. The size of a vector is the number of elements that it contains, which is directly controlled by how many elements you put into the vector.

Capacity is the amount of total space that the vector has. Under the hood, a vector just uses an array. The capacity of the vector is the size of that array. This is always equal to or larger than the size. The difference between them is the number of elements that you can add to the vector before the array under the hood needs to be reallocated.

You should almost never care about the capacity. It exists to let people with very specific performance and memory constraints do exactly what they want.

Hollyhock answered 9/6, 2011 at 17:38 Comment(10)
+1 clear to understand for me and also extra imp. info-->"You should almost never care about the capacity. It exists to let people with very specific performance and memory constraints do exactly what they want." ThanksCheyney
Disagree about not caring about capacity. If you know you have a minimum 200 items to be stored in your vector, you'd be crazy not to tell it that whilst constructing.Flawed
Crazy? I don't necessarily think so. I just ran a few tests, inserting 200 ints with and without reserving space, and I needed over 100,000 repetitions before reserving space showed a significant performance improvement. Granted, the performance was about twice as good when space was reserved, but unless you're doing this in a tight loop and this is your performance bottleneck, I don't really think it's crazy to not care very much about a couple of microseconds.Hollyhock
@John: so even with the knowledge that a re-allocation would be likely you'd still not pass on a single known number to the constructor in order to prevent said re-allocation? I stand by my "crazy". Even with ints on a modern machine (best case scenario test) I'd do it to make the intentions of the code clearer, never mind larger structs on an embedded system.Flawed
@Kent: I don't disagree with the principle. I think reserving capacity is good practice if you're in the very specific situation where you have definitive knowledge about your container's end use. That is why the "never" in my answer reads "almost never".Hollyhock
The number of allocations is going to be logarithmic relative to the number of elements. This is almost always negligible. It does not even matter how many elements the vector will contain.Pyaemia
@Don: Indeed. But the total number of item copies is O(N).Lunar
Note that there are more reasons to care about capacity than performance or "good practices". You might also care about iterator invalidation: if you reserve N elements and pass a pointer to &vec[0], then you can insert up to N elements without invalidating that pointer.Larue
Is the 'capacity' any different from 'max_size' ?Nun
@Nun max_size is the largest that size can get on that platform. Often that is only a theoretical limit, because no one has enough RAM in one machine to fill 64 bits of address spaceVermicelli
F
41

Size: the number of items currently in the vector

Capacity: how many items can be fit in the vector before it is "full". Once full, adding new items will result in a new, larger block of memory being allocated and the existing items being copied to it

Flawed answered 9/6, 2011 at 17:37 Comment(3)
I'd say not allocate but re-allocate. Because vector guarantees that data is continuously laid out in memory, it cannot use more than one memory blocks returned by operator new. So when the limit is hit, vector will allocate a new chunk of memory and copy existing data to that memory, then delete the previously allocated block. Or, if it is smart enough, it will use realloc function to be a bit more optimal.Lanfranc
@Vlad: I'm assuming you commented before seeing my last edit?Flawed
thanks this answer helped me understand it easier than the accepted answer. I've created this pastebin for this answer for future visitors: pastebin.com/Xx1RAc9K , also the example code here cplusplus.com/reference/vector/vector/reserve is much clearer now.Sepalous
M
29

Let's say you have a bucket. At most, this bucket can hold 5 gallons of water, so its capacity is 5 gallons. It may have any amount of water between 0 and 5, inclusive. The amount of water currently in the bucket is, in vector terms, its size. So if this bucket is half filled, it has a size of 2.5 gallons.

If you try to add more water to a bucket and it would overflow, you need to find a bigger bucket. So you get a bucket with a larger capacity and dump the old bucket's contents into the new one, then add the new water.

Capacity: Maximum amount of stuff the Vector/bucket can hold. Size: Amount of stuff currently in the Vector/bucket.

Mop answered 9/6, 2011 at 17:43 Comment(5)
You must be an instractor. I couldn't understand meaning of allocated memory exactly but I well understand now what it means exactly.Sphygmograph
That is a really great analogy! Vectors are buckets of water!Cardew
This explanation is completely wrong. I resize an empty vector to 3, and both size and capacity gave 3. I didn't put anything yet.Lookeron
@eigenfeld But if you reserve() an empty vector to 3, the size is 0 and the capacity is 3.Dipstick
@truthadjustr you already put something, when you resizeed.Bedmate
T
8

Size is number of elements present in a vector

Capacity is the amount of space that the vector is currently using.

Let's understand it with a very simple example:

using namespace std;

int main(){
  vector<int > vec;
  vec.push_back(1); 
  vec.push_back(1); 
  vec.push_back(1); 
  cout<<"size of vector"<<vec.size()<<endl;
  cout<<"capacity of vector"<<vec.capacity()<<endl;
  return 0;
}

currently size is 3 and capacity is 4.

Now if we push back one more element,

using namespace std;
  int main(){
  vector<int> vec;
  vec.push_back(1); 
  vec.push_back(1); 
  vec.push_back(1); 
  vec.push_back(1);
  cout<<"size of vector"<<vec.size()<<endl;
  cout<<"capacity of vector"<<vec.capacity()<<endl;
  return 0;
}

now size is: 4 capacity is 4

now if we try to insert one more element in vector then size will become 5 but capacity will become 8.

it happens based on the datatype of vector, as here in this case vector in of type int, as we know size of int is 4 bytes so compiler will allocate 4 block of memory ..and when we try to add 5th element , vector::capacity() is doubled what we have currently.

same keep on..for example : if we try to insert 9th element then size of vector will be 9 and capacity will b 16..

Trafficator answered 18/8, 2016 at 17:15 Comment(4)
I think your explanation of why the capacity gets doubled is vague. The number returned by capacity is about the number of "elements" we can add before reallocation and not about bytes. capacity of a vector of integers with two numbers in it is 2, if this 2 were in bytes, then even a single integer could not fit in the vector. Maybe I'm missing a point. Can you explain why the capacity doubles more clearly?Sepalous
My problem is worse. MSVC is returning the same number for both irrespective of how many elements I add😅Mouldon
I was using MS VStudio 17 but size and capacity are same for me in all casesDreyfus
@Sepalous this will help #54890021Glycosuria
L
6

size() tells you how many elements you currently have. capacity() tells you how large the size can get before the vector needs to reallocate memory for itself.

Capacity is always greater than or equal to size. You cannot index beyond element # size()-1.

Lunar answered 9/6, 2011 at 17:38 Comment(0)
D
5

The size is the number of elements in the vector. The capacity is the maximum number of elements the vector can currently hold.

Deneendenegation answered 9/6, 2011 at 17:38 Comment(0)
P
0

The vector size is the total number of elements of a vector and it is always the same for all compilers. Vectors are re-sizeable.

The capacity is the maximum number of elements the vector can currently hold. It may differ for different compilers.

Capacity changes if it needs to, or you can set an initial capacity and it will not resize until that capacity is reached. It is automatically expanded.

Capacity > = Size

Petrarch answered 9/6, 2021 at 1:22 Comment(0)
B
0

One is more of an important interface and the other is more of an important implementation detail. You will mostly deal with size and not capacity. In other words:

  • Size is the number of items in the vector. If you want to iterate through the vector, you need to know its size.

  • Capacity is how many items can be fit in the vector before more memory must be allocated to it. Once the capacity limit is reached, more memory is allocated to the vector.

An analogy to size is the number of balls in a box whereas the capacity is the box size. When programming, you normally want to know how many balls are in the box. The vector implementation should handle the capacity for you (making a bigger box once it is full).

enter image description here

Beholden answered 10/8, 2022 at 19:28 Comment(0)
D
0

To give a more visual answer to what have been described above. Try to see the change in memory location whenever the vector's capacity changes.

Memory location changes of the vector

Dahlgren answered 3/8, 2023 at 14:44 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewProthalamium

© 2022 - 2024 — McMap. All rights reserved.