Updated May 31, 2023
Introduction to C++ reserve()
The C ++ reserve() function helps us reserve a vector capacity. This capacity must be enough to contain n number of elements. This function can help us increase the capacity of any given vector with a value greater than or equal to the new capacity,, which we will specify in the reserve function. The reserve function will reserve the vector space but will not increase its size. If the size you reserve in the vector exceeds the size,, then all these changes stand invalidated.
Syntax
void reserve (size_type n)
Here n signifies the number of elements that will be stored in the vector. It will not return any value, but it will reserve the space in the memory. The resulting capacity can be equal to or greater than n. size_type is a type that is an unsigned integral type. This can also be referred to as size_t.
How reserve() function work in C ++?
Let us check the working of reserve() function in C ++.
// vector::reserve
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> example;
szc = example.capacity();
example.reserve(100);
std::cout << " Let us change the size of sample:\n:\n";
for (int i=0; i<100; ++i) {
example.push_back(i);
if (szc!=example.capacity()) {
szc = example.capacity();
std::cout << "Capacity of example is changed to: " << szc << '\n';
}
}
}
The reserve function here is reserving 100 bytes. This is allocated in the system once the reserve function is called. The capacity of this variable changes internally. You can then keep assigning values to this variable until this size is full. This function will allocate the said memory beforehand. We will check the working of this function with more examples as below.
Examples
Here are the following examples mentioned below.
Example #1
Code:
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
vector<int> vec1;
vector<int> vec2;
ssize_t size;
size = vec1.capacity();
for (int i = 0; i < 25; ++i) {
vec1.push_back(i);
if (size != vec1.capacity()) {
size = vec1.capacity();
cout << "Increasing the size limit of vector 1 so that it holds" << size
<< " elements" << endl;
}
}
cout << endl << endl;
//Here we will reserve space for 35 elements using reserve() function
vec2.reserve(35);
for (int i = 0; i < 25; ++i) {
vec2.push_back(i);
if (size != vec2.capacity()) {
size = vec2.capacity();
cout << "Increasing the size limit of vector 2 so that it holds " << size
<< " elements" << endl;
}
}
return 0;
}
The above code is an example of comparing reserve() and allocating space separately. We initially used libraries iostream and vector. In the main function, we have taken two vectors.
In the first case, we have used a for loop, which allocates the capacity until it reaches 35. We are using post increment in the loop. As we have not used a reserve() function in the first case, the capacity function can allocate space greater than 35.
While in the second case, we are again using for loop until it reaches capacity. The difference that you observe here is we have made use of the vector function reserve(). In this case, will you see that as space is already reserved hence, it will not allocate the space again and again, unlike the first case? As space is already allocated, there will not be multiple allocations. Observe the output so that you understand this functionality better.
Output:
You will see that space increases in the first case. In the second case, space is allocated at one go.
Example #2
Code:
#include <cstddef>
#include <new>
#include <vector>
#include <iostream>
// minimal C++11 allocator with debug output
template <class spc>
struct LetAlloc {
typedef spc value_type;
LetAlloc() = default;
template <class Ed> LetAlloc(const LetAlloc<Ed>&) {}
spc* allocate(std::size_t n)
{
n *= sizeof(spc);
std::cout << "Let us allocate space " << n << " bytes\n";
return static_cast<spc*>(::operator new(n));
}
void deallocate(spc* p, std::size_t n)
{
std::cout << "Let us deallocate space " << n*sizeof*p << " bytes\n";
::operator delete(p);
}
};
template <class Ed, class Up>
bool operator==(const LetAlloc<Ed>&, const LetAlloc<Up>&) { return true; }
template <class Ed, class Up>
bool operator!=(const LetAlloc<Ed>&, const LetAlloc<Up>&) { return false; }
int main()
{
int spsz = 100;
std::cout << "We are reserving space here by using reserve function: \n";
{
std::vector<int, LetAlloc<int>> vec1;
vec1.reserve(spsz);
for(int n = 0; n < spsz; ++n)
vec1.push_back(n);
}
std::cout << "Here we are using usual space allocation: \n";
{
std::vector<int, LetAlloc<int>> vec1;
for(int n = 0; n < spsz; ++n)
vec1.push_back(n);
}
}
In the above function, we have created a structure which is allocating and deallocates space using the structure and template format. Similarly, we are also deallocating the space by using the delete function. Secondly, we have used the reserve function, which very easily allocates the space as specified. We have defined a variable known as spsz with the size allocated as 100. Observe the below output to understand better.
Output:
Conclusion
The reserve() function in CPP is a very useful function of the vector library. It helps in allocating space and reserving it. These act as vectors that can store without any further reallocation. Once the reserved space is full the library will allocate fresh memory, and also it will copy all existing elements. It is a faster and more efficient way to reserve and use space when required. As vectors are dynamic, this is a way in which you can store space in advance.
Recommended Articles
This is a guide to the C++ reserve(). Here we discuss how to reserve() function work in C ++ with respective examples for better understanding. You may also have a look at the following articles to learn more –