Open In App

What happens whenever a C++ vector expands/reallocate on stack memory?

Last Updated : 01 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Vectors are sequence containers representing arrays that don’t require any size declaration and can change their size. Vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using indexing. They have the ability to change their size dynamically, with their storage being handled automatically by the container.

Syntax:

vector<data_type> v
 

Declaring a vector:

Some ways of declaring a vector are provided below

C++




#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // The length of the declaration
    // does not need to be specified
    vector<int> array1;
  
    // Initialize the array by using an
    // initializer list (before C++11)
    vector<int> array2 = { 9, 7, 5, 3, 1 };
  
    // Initialize arrays uniformly as with array,
    vector<int> array3{ 9, 7, 5, 3, 1 };
  
    // The type can be omitted since C++17
    // Deduced to vector<int>
    vector array4{ 9, 7, 5, 3, 1 };
}


Note: Due to vector’s ability to dynamically allocate memory as requested, both the uninitialized and initialized cases don’t require the array length at compile time.

Length and capacity in vectors:

Here is an example:   int* array{ new int[20] { 1, 2, 3, 4, 5, 6, 7, 8} };

Despite only using eight of the allocated elements, we would say that this array has a length of 20. Vectors contain two attributes length and capacity, unlike built-in arrays and arrays, which only remember their size. 

  • Length refers to the number of elements in the array,  
  • Capacity refers to the number of elements allocated to memory.

Below is a code example of that

C++




#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    vector<int> arr{ 0, 1, 2, 3, 4, 5, 6 };
  
    // The length is set to 5
    arr.resize(5);
    cout << "The length is: " << arr.size() << endl;
    cout << "The capacity is: " << arr.capacity();
    return 0;
}


Output

The length is: 5
The capacity is: 7

For more about vectors, you can refer to “Vector in C++ STL“.

A vector on stack(memory):

Let’s consider an example to understand how memory allocation is happening in vector

vector <Type> vector1; 

Vector named ‘vector1‘ in this case is allocated on stack, as it is defined as a local stack variable but the elements on the free store which is heap.

vector1 stored in stack

vector1 stored in stack

vector <Type*> vector1; 

Pointers are stored on heap, because the amount of the pointers can change dynamically and the vector named ‘vector1’ will be stored on the stack.

Vector1 will be on the stack its items (pointers to Type) will be on the heap.

Vector1 will be on the stack its items (pointers to Type) will be on the heap.

vector<Type> *vector1 = new vector<Type>;

Here the vector named ‘vector1‘ will be on the heap as well as the stack and use only heap to store the items.

Here, vector class will be on both heap and stack but the elements it internally store will be on the heap

Here, vector class will be on both heap and stack but the elements it internally store will be on the heap

What happens in case of Reallocation/Expansion of a vector’s size?

In case of an increase in the size of a vector, despite only being one memory position, the heap space allocated for data elements won’t suffice. So a new memory block is allotted for 2 components. The primary component and the new component will be copied/moved to the new storage, deallocating the old memory.

When vector space size is increased

When vector space size is increased

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads