struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
void deleteNode(ListNode* node) {
*(node) = *(node->next);
}
我知道会是这样
节点= 3
删除节点(节点)
1->2->3->4->5
1->2->4->5
但这会在哪里导致内存泄漏,指针将指向新节点,但 int 变量 3 是否仍会在内存中某处 float ?谢谢!
最佳答案
but would this lead to a memory leak
是的。
but would the int variable 3 still be floating around in memory somewhere?
没有。包含 4 的节点是被泄漏的节点,因为它是包含 3 的节点被覆盖,也就是指向 4 的节点。结果将是这样的:
4 <--- this is the node which was leaked (since nothing points to it)
\
->5
/
1->2->4 <--- this is the node that used to contain 3
关于c++ - 编写一个函数来删除单向链表中的节点(尾部除外),只允许访问该节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52838457/