提问人:Trung Pham 提问时间:10/7/2023 更新时间:10/7/2023 访问量:17
如何删除单向链表中节点内的指针
How to delete a pointer inside a node in a singly linked list
问:
我在删除节点内的对象指针时遇到问题。我想知道如果我只是删除一个节点,对象指针也会是免费的吗?
struct customer{
int data;
customer(int data){
this->data= data;
}
};
class Node{
public:
customer * c;
Node * next;
Node(customer *&x){
this->c = x;
this->next = nullptr;
}
};
int main() {
customer * khach = new customer(5);
Node * node = new Node(khach);
delete node;
}
答:
0赞
Marcin Orlowski
10/7/2023
#1
否,删除对象不会自动释放为该对象分配的内存。您需要在之前或之后显式设置,以避免内存泄漏。Node
customer
delete khach;
delete node;
评论