< Previous | Contents | Next >

Examining Vector Growth

Although vectors grow dynamically as needed, every vector has a specific size. When a new element added to a vector pushes the vector beyond its current size, the computer reallocates memory and might even copy all of the vector elements to this newly seized chunk of memory real estate. This can cause a performance hit.

The most important thing to keep in mind about program performance is whether or not you need to care. For example, vector memory reallocation might not occur at a performance-critical part of your program. In that case, you can safely ignore the cost of reallocation. Also, with small vectors, the reallocation cost might be insignificant so, again, you can safely ignore it. However, if you need greater control over when these memory reallocations occur, you have it.


Using the capacity() Member Function

The capacity() vector member function returns the capacity of a vectorin other words, the number of elements that a vector can hold before a program must reallocate more memory for it. A vectors capacity is not the same thing as its size (the number of elements a vector currently holds). Heres a code snippet to help drive this point home:

cout << "Creating a 10 element vector to hold scores.\n"; vector<int> scores(10, 0); //initialize all 10 elements to 0 cout << "Vector size is :" << scores.size() << endl;

cout << "Vector capacity is:" << scores.capacity() << endl;

Understanding Vector Performance 137


cout << "Adding a score.\n";

scores.push_back(0); //memory is reallocated to accommodate growth cout << "Vector size is :" << scores.size() << endl;

cout << "Vector capacity is:" << scores.capacity() << endl;

Right after I declare and initialize the vector, this code reports that its size and capacity are both 10. However, after an element is added, the code reports that the vectors size is 11 while its capacity is 20. Thats because the capacity of a vector doubles every time a program reallocates additional memory for it. In this case, when a new score was added, memory was reallocated, and the capacity of the vector doubled from 10 to 20.


Using the reserve() Member Function

The reserve() member function increases the capacity of a vector to the number supplied as an argument. Using reserve() gives you control over when a reallocation of additional memory occurs. Heres an example:

cout << "Creating a list of scores.\n";

vector<int> scores(10, 0); //initialize all 10 elements to 0 cout << "Vector size is :" << scores.size() << endl;

cout << "Vector capacity is:" << scores.capacity() << endl;


cout << "Reserving more memory.\n";

scores.reserve(20); //reserve memory for 10 additional elements cout << "Vector size is :" << scores.size() << endl;

cout << "Vector capacity is:" << scores.capacity() << endl;

Right after I declare and initialize the vector, this code reports that its size and capacity are both 10. However, after I reserve memory for 10 additional elements, the code reports that the vectors size is still 10 while its capacity is 20.

By using reserve() to keep a vectors capacity large enough for your purposes, you can delay memory reallocation to a time of your choosing.


Hin t

image

As a beginning game programmer, it’s good to be aware of how vector memory allocation works; however, don’t obsess over it. The first game programs you’ll write probably won’t benefit from a more manual process of vector memory allocation.

image

138 Chapter 4 n The Standard Template Library: Hangman