c++ 类内部 使用 new delete

低级错误:

 1 /**
 2 * @clusterNums 默认为5 分类数目
 3 * @xyVector 处理后点云数据 x y顺序存储 
 4 */
 5 int GMM::runGMM(int clusterNums, const std::vector<db::Point2f> & xyVector, std::vector<db::pointCuster> & xyCusterVector){
 6 
 7     int dim = 2;
 8 
 9     datanums = xyVector.size();
10     data = new double*[datanums];
11 
12     for (int i = 0, j = 0; j < xyVector.size(); j++, i++)
13     {
14         data[i] = new double[dim];
15         data[i][0] = xyVector[j].x;
16         data[i][1] = xyVector[j].y;
17     }
18     GMM gmm;
19     result = gmm.GMM_Cluster(data, datanums, dim, clusterNums);
20     //multimap<int, pair<double, double>>cluster;
21     vector<vector<int>>clusters;
22     clusters.resize(clusterNums);
23 
24     for (int i = 0; i < datanums; i++)
25     {
26         //cluster.insert(pair<int, pair<double, double>>
27         //    (result[i], pair<double, double>(data[i][0], data[i][1])));
28         clusters[result[i]].push_back(i);
29         //cout << result[i] << endl;
30     }
31 
32     for (int i = 0; i < clusterNums; i++)
33     {
34         db::pointCuster tempCuster;
35         xyCusterVector.push_back(tempCuster);
36 
37         for (int j = 0; j < clusters[i].size(); j++)
38         {
39             db::Point2f tempPoint;
40             xyCusterVector[i].push_back(tempPoint);
41 
42             xyCusterVector[i][j].x = data[clusters[i][j]][0];
43             xyCusterVector[i][j].y = data[clusters[i][j]][1];
44         }
45     }
46 
47     return 0;
48 }

 

总结:一定不要在类的内部,实例化它自己的对象,导致内存释放错误。

posted @ 2019-03-05 09:56  玥茹苟  阅读(654)  评论(0编辑  收藏  举报