[英]If I delete an array of pointers to nodes, will the nodes themselves be deleted?
假設我創建了一個指向節點的指針數組,每個節點都包含一個car對象成員變量,以便以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;
不,你只是釋放該內存a
本身指向。 不會以任何方式處理該內存的內容。
如果要自動“刪除”對象,請使用智能指針,或者更好地使用std::vector
對象 (而不是指向對象的指針)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.