只是想知道我删除这篇文章的尝试出了什么问题。因为我必须使用我的教授声明的变量作为
LN** map = nullptr;对于我正在处理的任务,使用更简单的数据类型是不可取的。
class LN {
public:
LN() : next(nullptr) {}
LN (const LN& ln) : value(ln.value), next(ln.next) {}
LN (int v, LN* n = nullptr) : value(v), next(n) {}
int value;
LN* next;
};
int main()
{
LN** array = nullptr;
array = new LN*[5];
int j=1;
for (int i=0; i<5; ++i) {
array[i] = new LN();
array[i] = new LN(j++, array[i]);
array[i] = new LN(j++, array[i]);
}
// What I think should work, but doesn't.
for (int i=0; i<5; ++i) {
delete array[i];
}
delete[] array;
array = nullptr;
return 0;
}发布于 2014-03-05 00:53:22
这里您的删除尝试没有任何问题。它将成功删除数组中当前存储的所有元素,然后是数组本身。
问题是LN的析构函数没有正确清理列表中的所有值。这会在删除head LN值时导致所有next指针泄漏。尝试在此处添加析构函数
~LN() {
delete next;
}https://stackoverflow.com/questions/22177939
复制相似问题