開發平台(Platform): (Ex: Win10, Linux, ...)
Linux
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
g++
#include<iostream>
#include<vector>
#include <memory>
using namespace std;
int main()
{
// M□NM□N matrix (assuming int type elements) initialized with all
values as KK
const int N = 2;
const int M = 4;
const int K = 99;
std::vector<std::vector<int>>* p = new std::vector<std::vector<int>>(M,
std::vector<int>(N, K));
cout << p[0].size() << endl;
cout << p[0][0].size() << endl;
cout << p->at(0)[0] << endl;
delete p;
// cout << p->at(0)[0] << endl;
return 0;
}
我不想用delete 手動回收new 記憶體! 想使用shared_ptr
請問要怎麼把vector 指標用shared_ptr 封裝一起自動回收?
shared_ptr<std::vector<std::vector<int> > > p1(new
std::vector<std::vector<int>>(M, std::vector<int>(N, 0)));
我這樣包裝對嘛? 有辦法驗證記憶體有回收掉?
謝謝