vector::reserve function


The reserve function of vector pre-allocate a memory of specified size.

void reserve(size_t n) ;

The reserved size is also the capacity of the vector.And the benefit of using this function is if you know how much memory is required before hand you can use this function to pre-allocate the required memory size.Pre-allocating the memory will prevent from reallocating the memory and copying of data later which is time consuming and can cause a bottleneck in the efficiency of the program.

Parameters:
n – Number of elements to reserve the storage for.

Return type
void

Note This function will throw the exception std::length_error if the reserve size requested is more than the size given by the max_size() function.

Code example:

vector<int> vec ;

vec.reserve( 50 ) ; ///reserve a memory to hold 50 integers

cout<< vec.capacity() ;

Output,

50

Here is another code showing that an exception is thrown when the you try to reserve size greater than the max_size.In Visual Studio the max_size() function will give you ‘1073741823’ ,try reserving a size greater than this value.Your compiler will throw an std:length_error exception .

Code example:

vec.reserve( 1073741824 ) ;

Another way to reserve a memory

Another way to reserve memory for vector is to pass an argument to the vector constructor during it’s creation.The argument passed is the required size that is to be reserved.

vector<int> vec(5) ;/// A memory is reserved for 5 integers

Difference between this method and calling the reserve() function

There is a difference between reserving a memory using this method and calling the reserve() function to allocate the storage.In this method the memory reserved will be initialized with certain values.If the type is int , float and char the memory gets initialized with 0.If the type is string the memory is initialized with empty string (“”).If the type is class it’s default constructor is called the number of times the value passed.

If however we call the reserve() function the memory is not initialized with any value.The memory is simply blank.And so trying to access the memory will render an undefined result.

vector<int> vec(5) ;/// A memory is reserved for 5 integers

cout<&lt vec[0] <&lt ” , ” <&lt vec[3] ;

vector< int < vec1 ;

vec1.reserve(4) ;

cout<&lt vec1[0] <&lt ” , ” <&lt vec1[3] ;

Output

0 , 0
undefined , undefined





Initializing the vector whose memory is reserved using the reserve( ) function

When the memory is reserve using the reserve() function the memory is not initialize with any value.If the type is a class then it’s default constructor is not called.To initialize the memory reserved you can call the push_back() function.

vector<int> vec;

vec.reserve( 4 );

cout<< vec[0] ;///undefined

//intializing the vector
vec.push_back(12 ) ; //the first element
vec.push_back(891 ) ;//the second element
vec.push_back ( 45 ) ;//the third element
vec.push_back( 77 ) ;//the fourth element

///outputting the data in vector
for(auto elem:vec )
{
cout&lt< elem << ” , ” ;
}

Output,

12 , 891 , 45 , 77

Here is another code example using class as it’s type.

class A
{
public:
int i ;
A(int ii):i(ii) { cout<< “A constructor \n”; }
};

int main()
{
vector<A > vecA ;

vecA.reserve( 4 ) ;

vecA.push_back( A(45) ); ///constructor called here

cout<< vecA[0].i ;

cin.get();
return 0 ;
}

Output ,

45


 


Stop wasting time,earn money($$$) from your website-Join Now!

Initialization using subscript is invalid

We cannot use the subscript ‘[]’ to initialize the vector whose memory is reserved by the reserve( ) function.Although it can be used for reading and writing a value in the memory ,we can only modify- meaning write- the value only after the memory has been initialized.However,reserving a memory using reserve( ) function does not initialize the memory.In other words the memory does not contain any value.If a memory is not initialized -does not contain any value- how can we expect to change the value,we cannot!

vector<int> vec;

vec.reserve( 4 );

vec[0]=90 ;//undefined
vec[3]=12 ; ///undefined

cout<< vec[0] << endl ///undefined
<< vec[3] ; //undefined





Using reserve( ) invalidates iterators, pointers and reference referring to the vector

Calling the reserve() function will reallocate a new storage.So, it ultimately follows that the memory of the vector will have new addresses.The iterators or pointers or reference refering to the previous memory will certainly get invalidated.I am sure it’s not a big surprise,it was bound to happen.

vector<int> vec(2);

vec[0]=89 ;
vec[1]=88 ;

vector<int>::iterator vecIt=vec.begin() ;

cout<< vecIt[0] << ” , ” << vecIt[1] << endl ;

//reserving new storage
vec.reserve( 4 );

vec.push_back(45) ;
vec.push_back(90) ;

cout<< vecIt[0] << endl ///undefined
<< vec[1] ; //undefined

Output,

89 , 88
undefined value
undefined value


Argument value lesser than the size of the vector

Calling a reserve( size_t ) function with the argument value less than the vector size is a nonbinding call.Nonbinding call means the compiler can neglect the function call.Reserving a size less than the current size of the vector is insensible ,obviously the call is bound to be nonbinding.

Link:: vector::push_back( ) function

Code example

vector<int> vec;

vec.reserve(3);

vec.push_back( 12 );

cout<< vec.capacity( ) << endl
<< vec.size( ) << endl ;

/*Reserving size more than the current capacity */
vec.reserve( 6 ) ;

cout<< vec.capacity( ) << endl
<< vec.size( ) << endl ;

/*Reserving size less than the current capacity */
vec.reserve(2) ;

cout<< vec.capacity( ) << endl
<< vec.size() << endl ;

Output

3
1
6
1
6
1

The call vec.reserve(2) is neglected by the compiler.


Related Link

->vector::capacity function