Difference between Capacity and Size of an Array

Hello I just wanted yo know the differece between the capacity and the size of an array.

Here is how my question starts and I need to know the difference, to understand the question.

Consider an array A of type T, capacity N and size S where S is always smaller than N.


Thanks
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    // ...

    // an array A of type T, capacity N and size S where S is always smaller than N.
    const std::size_t N = 100 ; // capacity
    int /* type T */ A[N] ; // array
    std::size_t S = 0 ; // logical size (initially zero)

    // ...

    // append a value to the array
    const int value = 46 ;

    if( S < N ) // if there is space in the array (S is smaller than N)
    {
        A[S] = value ; // place the value at the end
        ++S ; // and increment the logical size
    }

    // ... 
int ar[100] = {0};
cout<< sizeof(ar); //ok 400

capacity ???
1
2
3
sizeof(ar) ; // size of the array in bytes ie. capacity * size of each element ie. 100 * sizeof(int)

sizeof(ar) / sizeof( ar[0] ) ; // number of elements in the array ie. capacity 
closed account (48T7M4Gy)
My interpretation FWIW:

Capacity: the number of elements of type T that the array is capable of storing. E.g. an array of 2000 widgets W[2000] has a capacity of 2000 widgets.

Size: If you only have 123 widgets stored, or intended to be stored then the size is 123. And, you can't store more than 2000 widgets.

A single widget has a certain size in memory but that is a different story.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Exploring the size and capacity of a vector
#include <iostream>
#include <vector>
using namespace std;

void showInfo( vector<int> v) 
{
cout << "Container capacity: " << v.capacity()
<< " size: " << v.size() << endl;
}

int main()
{
// Basic vector creation
vector<int> numbers; // Create a vector

// See how adding elements affects capacity increments
auto oldC = numbers.capacity(); // Old capacity
auto newC = oldC; // New capacity after adding element
showInfo(numbers);

for(int i =0; i<100; i++)
	{
	cout << "i=" <<i<<",";
	numbers.push_back(2*i); //add number at the end
	newC = numbers.capacity();
	if(oldC < newC) // Only output when capacity increases
		{	
		cout << "\n\n";
		oldC = newC;
		showInfo(numbers);
		cout << "\n";
		}
	}
}
-Beginning Visual C++ 2013 - Ivor Horton, page:496 (simplified little)

does capacity apply to fixed size array ??
Last edited on
closed account (48T7M4Gy)
does capacity apply to fixed size array ??


Yes, except the size and capacity are managed differently in the case of an ordinary array. A C-string might be declaclared as xyz[100] but only 10 elements followed by the null terminator apply in a particular case. So capacity = 100, size =10.

Vectors manage size and capacity 'automatically'.
http://www.cplusplus.com/reference/vector/vector/capacity/ might be of interest.
does capacity apply to fixed size array ??

Yes. The example by JLBorges has static array.

You have a jar. The jar has a fixed size. You have put 3 coins in it. If you want to take a coin, then you must choose only from those three coins, i.e. the logical size is 3, even though the jar has capacity to hold more coins.


There is a difference between vector and array though.
1
2
3
4
5
6
7
8
const size_t N = 7;
Foo bar[ N ];
bar[ 0 ] = Foo( "Hello", 42 );
size_t S = 1;

std::vector<Foo> gaz;
gaz.reserve( N );
gaz.emplace_back(  "Hello", 42 );

Both bar and gaz have (currently) capacity to hold N Foos.
Both bar and gaz have one Foo. (logical size == 1)

What can be said about bar[1] and gaz[1]?
capacity to hold N Foos

reserve(N) reserves for N+i elements //i>=0
Topic archived. No new replies allowed.