What happens whenever a C++ vector expands/reallocate on stack memory?
Last Updated :
01 Nov, 2022
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;
}
OutputThe 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 stackvector <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.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 heapWhat 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
Similar Reads
How can the stack memory be increased? A Stack is a temporary memory address space that is used to hold arguments and automatic variables during the invocation of a subprogram or function reference. The size of this stack is called the stack size. Stack â¤â¤ Heap BSS DATA TEXT How to increase stack size? One cannot increase the stack size.
2 min read
Stack vs Heap Memory Allocation In C, C++, and Java, memory can be allocated on either a stack or a heap. Stack allocation happens in the function call stack, where each function gets its own memory for variables. In C/C++, heap memory is controlled by programmer as there is no automatic garbage collection.Stack AllocationStack al
7 min read
How to Avoid Memory Leaks When Using a Vector of Pointers to Dynamically Allocated Objects in C++? In C++, managing memory properly is very important to avoid memory leaks, especially when working with dynamically allocated objects. When using a std::vector of pointers to dynamically allocated objects, we need to ensure that all allocated memory is properly deallocated. In this article, we will l
6 min read
Initializing Vector using an Existing Vector in C++ STL A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector<data_structure/type> vector_name(size, item) To know more about v
5 min read
How to Get Vector Pointer to Raw Data in C++? In C++, vector is a dynamic array that stores the data in the contiguous memory allocated during runtime. It internally keeps the pointer to this raw data memory. In this article, we will learn how to get pointer to the vector's raw data.The easiest method to get the pointer to raw data is by using
2 min read
Is std::vector So Much Slower Than Plain Arrays? In C++, std::vector is a dynamic array provided by the Standard Template Library (STL), and it is commonly compared to plain arrays. A frequent question that arises is: Is std::vector significantly slower than plain arrays? The straightforward answer is no, std::vector is not significantly slower, t
6 min read