C++ vector内存分配及正确释放_vector 释放-CSDN博客
内存分配
#include <iostream>
#include <vector>
using namespace std;int main(){
vector<int> vec(10);
cout << "vec.size: "<< vec.size() <<endl;
cout << "vec.capacity;"<< vec.capacity()<<endl;
vec.push_back(1);
cout << "vec.size: "<< vec.size() <<endl;
cout << "vec.capacity:"<< vec.capacity()<<endl;}
运行结果如图:
需要内存释放的场景
#include <iostream>
#include <vector>
using namespace std;void myFunc() {vector<int> vec(1000000, 123);
}int main() {myFunc();return 0;
}
#include <iostream>
#include <vector>
using namespace std; int main() {vector<int>* vec = new vector<int>(1000000, 123);vector<int>().swap(*vec);cout << "After swap - size: " << vec->size() << ", capacity: " << vec->capacity() << endl;delete vec;return 0;
}
#include <iostream>
#include <memory>
#include <vector>
using namespace std;int main() {unique_ptr<vector<int>> vec(new vector<int>(1000000, 123)); cout << "vec size: " << vec->size() << ", capacity: " << vec->capacity() << endl;cout << "离开作用域后,vec自动销毁,不需要swap释放内存" << endl;return 0;
}
内存释放
#include <iostream>
#include <vector>
using namespace std;int main(){vector<int> vec;//手动空间100 vec.reserve(100);vec.push_back(1);vec.push_back(2);cout <<"vec.size(): " << vec.size() << endl;cout <<"vec.capasity(): " << vec.capacity() << endl;vec.clear();cout <<"After vec.size(): " << vec.size() << endl;cout <<" After vec.capasity(): " << vec.capacity() << endl;return 0;
}
#include <iostream>
#include <vector>
using namespace std;int main(){vector<int> vec;//手动空间100 vec.reserve(100);vec.push_back(1);vec.push_back(2);cout <<"vec.size(): " << vec.size() << endl;cout <<"vec.capasity(): " << vec.capacity() << endl;vec.clear();vec.shrink_to_fit();cout <<"After vec.size(): " << vec.size() << endl;cout <<"After vec.capasity(): " << vec.capacity() << endl;return 0;
}
#include <iostream>
#include <vector>
using namespace std;int main(){vector<int> vec;//手动空间100 vec.reserve(100);vec.push_back(1);vec.push_back(2);cout <<"vec.size(): " << vec.size() << endl;cout <<"vec.capasity(): " << vec.capacity() << endl;vector<int> ().swap(vec);cout <<"After vec.size(): " << vec.size() << endl;cout <<"After vec.capasity(): " << vec.capacity() << endl;return 0;
}
#include <iostream>
#include <vector>
using namespace std;int main(){vector<vector<int>> vec;vec.push_back({1,2});vec.push_back({4,5,6});vec.push_back({7,8,9});cout<<"vec outer size: "<<vec.size()<<endl;for(const auto& i: vec){cout <<"vec vinner size: "<<vec.size() <<" , vec inner capacity: "<<i.capacity()<<endl;}//释放内层for(auto& i :vec){i.clear();i.shrink_to_fit();} //释放外层vec.clear();vec.shrink_to_fit();cout<<"after releasing vec outer size: "<<vec.size()<<endl;
}
方法二:逐层采用swap释放
#include <iostream>
#include <vector>
using namespace std;int main(){vector<vector<int>> vec;vec.push_back({1,2});vec.push_back({4,5,6});vec.push_back({7,8,9});cout<<"vec outer size: "<<vec.size()<<endl;for(const auto& i: vec){cout <<"vec vinner size: "<<vec.size() <<" , vec inner capacity: "<<i.capacity()<<endl;}//释放内层for(auto& i :vec){vector<int>().swap(i);} //释放外层 vector<vector<int>>().swap(vec);cout<<"after releasing vec outer size: "<<vec.size()<<endl;
}
方法三:重新分配,销毁整个双层vector释放
#include <iostream>
#include <vector>
using namespace std;int main(){vector<vector<int>> vec;vec.push_back({1,2});vec.push_back({4,5,6});vec.push_back({7,8,9});cout<<"vec outer size: "<<vec.size()<<endl;for(const auto& i: vec){cout <<"vec vinner size: "<<vec.size() <<" , vec inner capacity: "<<i.capacity()<<endl;}vector<std::vector<int>>().swap(vec);cout<<"after releasing vec outer size: "<<vec.size()<<endl;
}
#include <iostream>
#include <vector>
using namespace std;void res(const vector<vector<vector<int>>>& tmp) {cout << "最外层 vector size: " << tmp.size() << endl;for (const auto& i : tmp) {cout << "\中间层 vector size: " << i.size() << endl;for (const auto& j : i) {cout << "\t\t最内层 vector size: " << j.size() << endl;}}
}int main() {vector<vector<vector<int>>> vec;for (int i = 0; i < 3; ++i) {vector<vector<int>> vec1;for (int j = 0; j < 2; ++j) {vector<int> vec2;for (int k = 0; k < 4; ++k) {vec2.push_back(i * 100 + j * 10 + k);}vec1.push_back(vec2);}vec.push_back(vec1);}//三层veccout << "-----初始vec-----" << endl;res(vec);for (auto& vec1 : vec) {for (auto& vec2 : vec1) {vector<int>().swap(vec2); // 释放最深层}vector<vector<int>>().swap(vec1); // 释放中间层}vector<vector<vector<int>>>().swap(vec); // 释放最外层// 打印释放内存后的向量信息cout << "\n-----释放后vec-----" << endl;res(vec); // 中间层和最内层不会输出,因为内存已经被释放,编译器可能报错return 0;
}