簡體   English   中英

為包含STL類的鏈表結構釋放內存

[英]freeing up memory for a linked list structure containing STL classes

我有一個由結構定義的鏈表

struct node {
    std::string elem;
    std::vector<node *> children;
};

假設已正確動態分配了該堆,之后如何釋放堆? 我正在嘗試:

void removeNodes(node* hElem) {
   for (node *hElemChildren: hElem->children)
       removeNodes(hElemChildren);
   delete hElem;
}

但是我擔心在此過程中,我會丟失動態分配給std::stringstd::vector內存。 有人可以讓我知道這里是否有內存泄漏嗎? 謝謝!

這里沒有內存泄漏。 當您調用delete時,將調用此元素的析構函數,這將分別為std::stringstd::vector調用析構函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM