[問題] std::shared_ptr std::vector
開發平台(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)));
我這樣包裝對嘛? 有辦法驗證記憶體有回收掉?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 175.182.108.242
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1506609628.A.FD2.html
※ 編輯: shihyu (175.182.108.242), 09/28/2017 22:45:57
推
09/28 22:47, , 1F
09/28 22:47, 1F
推
09/28 23:07, , 2F
09/28 23:07, 2F
→
09/28 23:11, , 3F
09/28 23:11, 3F
推
09/28 23:19, , 4F
09/28 23:19, 4F
→
09/28 23:39, , 5F
09/28 23:39, 5F
→
09/28 23:39, , 6F
09/28 23:39, 6F
→
09/28 23:39, , 7F
09/28 23:39, 7F
→
09/28 23:40, , 8F
09/28 23:40, 8F
推
09/29 00:02, , 9F
09/29 00:02, 9F
→
09/29 00:02, , 10F
09/29 00:02, 10F
→
09/29 00:03, , 11F
09/29 00:03, 11F
→
09/29 00:04, , 12F
09/29 00:04, 12F
→
09/29 00:04, , 13F
09/29 00:04, 13F
→
09/29 00:12, , 14F
09/29 00:12, 14F
→
09/29 00:13, , 15F
09/29 00:13, 15F
→
09/29 00:32, , 16F
09/29 00:32, 16F
→
09/29 01:02, , 17F
09/29 01:02, 17F
推
09/29 01:46, , 18F
09/29 01:46, 18F
→
09/29 01:49, , 19F
09/29 01:49, 19F
推
09/29 03:44, , 20F
09/29 03:44, 20F
→
09/29 06:23, , 21F
09/29 06:23, 21F
→
09/29 06:23, , 22F
09/29 06:23, 22F
→
09/29 06:24, , 23F
09/29 06:24, 23F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):