gpt4 book ai didi

C链表内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:24 25 4
gpt4 key购买 nike

我想不通为什么我的链表有内存泄漏。

我有一个名为 insertAtFront() 的方法

void insertAtFront(Node **head, char *key) {
Node *new = malloc(sizeof(Node));
if (!new) fatal("Malloc of new Node failed");
new->word = key;
new->next = *head;
*head = new;
}

然后我有一个释放节点的 removeAtFront() 方法。

void removeAtFront(Node **head) { 
Node *newhead = (*head)->next;
free(*head);
*head = newhead;
}

如果我只添加一个节点然后将其删除,则不会发生泄漏。如果我删除了多个节点,valgrind 会显示与添加的额外节点数量成比例的泄漏。我真的不明白为什么在释放节点时会发生这种情况。有什么想法吗?

最佳答案

您的问题似乎是生命周期问题。您没有发布调用代码和完整的可验证示例。节点已正确释放,但有些地方没有。 key 参数是否已分配但未在其他地方释放?

当您释放节点时,您不会释放 word 成员。如果 key 参数是由调用 insertAtFront() 的函数分配的,它应该随节点一起释放。如果 word 字段仅在某些时候被分配,则很难确定何时释放它。

一个好的解决方案是 insertAtFont() 总是复制 key 字符串,而 removeAtFront() 总是释放 单词成员:

void insertAtFront(Node **head, const char *key) {
Node *new = malloc(sizeof(*new));
if (!new) fatal("Malloc of new Node failed");
new->word = strdup(key);
if (!new->word) fatal("Malloc of Node key failed");
new->next = *head;
*head = new;
}

void removeAtFront(Node **head) {
Node *node = *head;
if (node) {
*head = node->next;
free(node->word);
free(node);
}
}

请注意,insertAtFront() 可以方便地免费返回指向新插入节点的指针。

关于C链表内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35420629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com