c - 无法释放所有已用内存

标签 c list linked-list

自动评分器告诉我,我未能释放所有已用内存。我不确定我在哪里导致了内存泄漏,所以这是我的整个代码:

struct lnode {
int count;
int line;
char* word;
struct lnode* next;
};


struct lnode* newNode(char* word, int line) {
struct lnode* temp = (struct lnode*)malloc(sizeof(struct lnode));
char* newWord = (char*)malloc(strlen(word) + 1);
newWord = strcpy(newWord, word);
temp->word = newWord;
temp->line = line;
temp->count = 1;
return temp;
}

void pushNode(struct lnode** head, struct lnode* node) {
node->next = *head;
*head = node;


}

struct lnode* getNode(struct lnode* head, char* word) {
struct lnode* current = head;
char* temp = (char *)malloc(strlen(word));
strcpy(temp, word);
while(current != NULL) {
    if(!strcmp(nodeGetWord(current),temp)) 
        return current; 

    current = nodeGetNext(current);
}
return NULL;
}

char* nodeGetWord(struct lnode* node) {
return node->word;
}

struct lnode* nodeGetNext(struct lnode* node) {
return node->next;
}

int nodeGetLine(struct lnode* node) {
int line = node->line;
return line;
}

int nodeGetCount(struct lnode* node) {
return node->count;
}

void nodeSetCount(struct lnode* node, int count) {
node->count = count;
}

void nodeSetLine(struct lnode* node, int line) {
node->line = line;
}

void deleteList(struct lnode** head) {
struct lnode* current = *head;
struct lnode* next;
while(current) {
    next = current->next;
    free(current);
    current = next;
}
*head = NULL;

}

void deleteNode(struct lnode** head, struct lnode* node) {
struct lnode* currentNode = *head;
struct lnode* previousNode = NULL;

while (currentNode != NULL) {
    if (currentNode != node) {
        previousNode = currentNode;
        currentNode = nodeGetNext(currentNode);
        continue;
    }

    if (previousNode)
        previousNode->next = node->next;
    else
        *head = node->next;
    free(node);
    break;
}
}

void printList(struct lnode** head) {
struct lnode* current = *head;
while (current != NULL) {
    printf("%s\n",nodeGetWord(current));
    current = nodeGetNext(current);
}

} 
int main() {
struct lnode* head = NULL;

struct lnode* a = newNode("Hello",3);
pushNode(&head, a);
struct lnode* b = newNode("Hi",2);
pushNode(&head, b);
struct lnode* c = newNode("Hola",4);
pushNode(&head, c);
struct lnode* d = newNode("Yo",5);
pushNode(&head, d);
struct lnode* e = newNode("Bye", 7);
pushNode(&head, e);
printList(&head);
//deleteNode(&head,e);
//printf("key: %s\n",nodeGetWord(e));
//printf("\n");
deleteList(&head);
printf("\n");
printList(&head);   
printf("\nDone\n");


}

main 和 printList() 函数可以忽略,因为当我将其提交给自动评分器时,这些函数被注释掉了——它们仅用于测试目的。一切似乎对我来说都很顺利。我什至实现了一个全局整数,每当我malloc某些东西时它就会更新,并且每当某些东西被释放时它就会递减。如果有人能指出可能存在内存泄漏的地方,那就太好了!

最佳答案

你期望节点的 printf 做什么?为什么您期望从列表中删除某些内容会影响删除内容的打印?

除了释放节点这一事实之外,从列表中删除节点并不相关......您正在打印节点,而不是列表。您正在打印的节点是您释放的节点,这是一种未定义的行为。没有办法知道可能是什么行为。对于您的实现,它恰好会打印在您释放之前节点的值,但您不能指望它或类似的东西。

编辑:

The autograder is telling me that I failed to free all used memory.

您为单词分配了内存,但从未释放它,您只释放了节点。您应该用 freeNode 替换两个 free 调用,并编写 freeNode 来释放该节点拥有的任何内存,在本例中为 free(node->word) 和 free(node)。

关于c - 无法释放所有已用内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594181/

相关文章:

c# - 有效地将多个元素添加到C#中List的开头

c++ - Floyd 算法 - 循环检测 - 不终止的例子

c - 无法从链表中弹出顶部元素

c - 我应该在 C 语言中长时间使用 sleep() 函数吗?

c - 如何内联另一个翻译单元的函数?

c - 进入无限循环的简单 C 代码。为什么?

r - 合并大量逻辑向量

python - 在 Python 中将字符串转换为所有非字母数字字符的列表

c - 我无法使用链表打印多项式

c - ELF 二进制文件中默认信号处理程序的代码在哪里?