Delete vector contents and free up memory in C++
This post will discuss how to delete the vector’s contents and free up the memory allocated by the vector to store objects in C++.
1. Using vector::clear
function
We can use the vector::clear
function to remove all elements from the vector. It works by calling a destructor on each vector object, but the underlying storage is not released. So, we’re left with a vector of size 0 but some finite capacity.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5 };
v.clear();
std::cout << "The vector size is " << v.size() << ", and its "
<< "capacity is " << v.capacity();
return 0;
}
|
Output:
The vector size is 0, and its capacity is 5
Starting with C++11, we can call the vector::shrink_to_fit
function after clear()
, which reduces the vector’s capacity to fir the size. It works by “requesting” a reallocation on the vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5 };
v.clear();
v.shrink_to_fit();
std::cout << "The vector size is " << v.size() << ", and its "
<< "capacity is " << v.capacity();
return 0;
}
|
Output:
The vector size is 0, and its capacity is 0
2. Using vector::erase
function
Another solution is to call the vector::erase
function on the vector, as shown below. This also suffers from the same problem as the clear()
function, i.e., reallocation of memory is not guaranteed to happen on the vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5 };
v.erase(v.begin(), v.end());
v.shrink_to_fit();
std::cout << "The vector size is " << v.size() << ", and its "
<< "capacity is " << v.capacity();
return 0;
}
|
Output:
The vector size is 0, and its capacity is 0
3. Using vector::resize
function
We can also use the vector::resize
function for resizing the vector. Notice that this function does not destroy the vector objects, and all references to objects remain valid.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5 };
v.resize(0);
v.shrink_to_fit();
std::cout << "The vector size is " << v.size() << ", and its "
<< "capacity is " << v.capacity();
return 0;
}
|
Output:
The vector size is 0, and its capacity is 0
4. Using vector::swap
function
All the above solutions fail to release the memory allocated to the vector object without calling the vector::shrink_to_fit
function. The shrink_to_fit()
shrinks the capacity to fit the size, but we can’t entirely rely on it. This is because it makes a non-binding request, and the vector implementation is free to ignore it completely.
There is another workaround to free the memory taken by the vector object. The idea is to swap the vector with an empty vector (with no memory allocated). This will de-allocate the memory taken by the vector and is always guaranteed to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5 };
std::vector<int>().swap(v);
std::cout << "The vector size is " << v.size() << ", and its "
<< "capacity is " << v.capacity();
return 0;
}
|
Output:
The vector size is 0, and its capacity is 0
That’s all about deleting vector contents and free up memory in C++.
Thanks for reading.
Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and help us grow. Happy coding :)
南来地,北往的,上班的,下岗的,走过路过不要错过!
======================个性签名=====================
之前认为Apple 的iOS 设计的要比 Android 稳定,我错了吗?
下载的许多客户端程序/游戏程序,经常会Crash,是程序写的不好(内存泄漏?刚启动也会吗?)还是iOS本身的不稳定!!!
如果在Android手机中可以简单联接到ddms,就可以查看系统log,很容易看到程序为什么出错,在iPhone中如何得知呢?试试Organizer吧,分析一下Device logs,也许有用.