std::vector memory

Basics

The vector class in C++ is like a resizable array, a template class in a standard template library. Consider the following Student class.

Student empty has an age 0 with height 0 
Student empty has an age 0 with height 0
classRoomSeat has a size of 7

2 ways of vector iteration

One way is to use an iterator object to overload the * operator and access the iterator like a pointer by dereferencing it. The alternative way is just a simple old school index base for loop iteration.

Student empty has an age 0 with height 0 
Student empty has an age 0 with height 0
Student empty has an age 0 with height 0
Student empty has an age 0 with height 0
Student empty has an age 0 with height 0
Student Kevin has an age 18 with height 180.2
Student Mary has an age 18 with height 158.9

Memory in vector

A vector manages an array internally.

If we add more elements to the vector that the internal array can currently hold, the vector class will create another array that is bigger and copies all the elements from the old array to the new array. The vector has a method called capacity, which is the size of the internal array that the current vector is using. Consider the following code and result to see how the internal array changes.

Initial vector size = 20
Initial internal array size = 20
i = 0, vector size = 21, internal array size = 40
i = 20, vector size = 41, internal array size = 80
i = 60, vector size = 81, internal array size = 160
i = 140, vector size = 161, internal array size = 320
i = 300, vector size = 321, internal array size = 640
i = 620, vector size = 641, internal array size = 1280
i = 1260, vector size = 1281, internal array size = 2560
i = 2540, vector size = 2561, internal array size = 5120
i = 5100, vector size = 5121, internal array size = 10240

Some vector methods to mention

.clear() method

We can clean up the vector by using .clear() method where all contents in the vector will be removed. But let us see what is going inside the internal array. Consider the following code from above.

Initial vector size = 20
Initial internal array size = 20
i = 0, vector size = 21, internal array size = 40
i = 20, vector size = 41, internal array size = 80
i = 60, vector size = 81, internal array size = 160
i = 140, vector size = 161, internal array size = 320
i = 300, vector size = 321, internal array size = 640
i = 620, vector size = 641, internal array size = 1280
i = 1260, vector size = 1281, internal array size = 2560
i = 2540, vector size = 2561, internal array size = 5120
i = 5100, vector size = 5121, internal array size = 10240
vector size = 0
internal array size = 10240

.resize() method

There is another method that I also want to mentioned and it is the .resize() method. It resizes the vector but has no effect on the internal array size. Consider the code below and the result from above (I will not repeat the code above again here).

2
vector size = 100
internal array size = 10240

.reserve() method

But how about the internal array? What if we want to modify the current internal array size? Consider the code below to see how .reserve() help us to deal with it. This method also creates a new internal array and copy all the elements from the old array to the new array.

internal array size before reserve = 10240
2
vector size = 10020
internal array size = 20000

Conclusion

All in all, I have explained how the vector template class in the standard template library manages an array using the new keyword behind, as well as some methods that worth digging deep to see how the memory works behind. Nowadays many high-level languages provide many convenient features for programmers. As a good software engineer, we should always know deeply about how the language works and its nature.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin Cheung

Kevin Cheung

𝑾𝒉𝒂𝒕 𝑰 𝒄𝒂𝒏𝒏𝒐𝒕 𝒄𝒓𝒆𝒂𝒕𝒆, 𝑰 𝒅𝒐 𝒏𝒐𝒕 𝒖𝒏𝒅𝒆𝒓𝒔𝒕𝒂𝒏𝒅. . An ordinary developer records his life and learning progress.