假设我创建了一个指向节点的指针数组,每个节点都包含一个汽车对象成员变量,以便按照mpg的降序打印出来。一旦我按照MPG的降序对数组进行排序并打印出最终结果,我想删除我创建的指针数组。
最后删除数组是否也会删除单链表中的节点?
cout << "\nCARS RANKED BY MPG\n-------------\n";
int capacity = count; //number of nodes in the list
Node **a = new Node*[capacity];
int numOfElem = 0;
Node *currentCar = first; //create a pointer to the first node in the list
while (numOfElem < count)
{
a[numOfElem++] = currentCar; //populate the array of pointers
currentCar = currentCar->getLink();
}
//Do something....
delete[] a; //delete array of pointers
a = nullptr;
答案 0 :(得分:1)
不,你只是释放了a
本身所指向的记忆。不以任何方式处理该内存的内容。
如果您想要自动“删除”对象,请使用智能指针,或者更好的std::vector
对象(不是指向对象的指针)。