【发布时间】:2014-04-17 06:55:38
【问题描述】:
所以我在下面创建两个不同的多维向量
string **customdiceArray = new string*[crows];
for(unsigned int i = 0; i<crows;i++){
customdiceArray[i] = new string[ccolumns];
}
然而,他们给了我内存泄漏。我对两个向量都进行了如下所示的删除调用。我哪里错了?
//Deallocate create objects
delete diceArray;
diceArray = 0;
//set<string>* words = new set<string>;
delete words;
words = 0;
//string **customdiceArray = new string*[crows];
delete customdiceArray;
customdiceArray = 0;
【问题讨论】:
-
这些是数组。不是向量。
-
你必须遍历数组删除每个元素,然后删除数组。
-
像这样:
std::vector<std::vector<std::string>> dice(crows, std::vector<std::string>(ccolumns)); -
好的 C++ 程序几乎不包含
delete语句。如果您实际上使用vector而不是数组,您的代码会更简单和正确。
标签: c++ vector multidimensional-array memory-leaks