0

自动分级机告诉我我未能释放所有使用的内存。我不确定我在哪里造成了内存泄漏,所以这是我的整个代码:

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,而每当有东西被释放时它就会递减。如果有人能指出可能的内存泄漏在哪里,那就太好了!

4

2 回答 2

1

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

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

编辑:

自动分级机告诉我我未能释放所有使用的内存。

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

于 2012-09-26T04:19:08.147 回答
0

我猜nodeGetWord只是从传递给它的节点中提取文本字符串,这就是为什么在删除节点后它仍然打印正确的字符串。它根本不看清单。

但是,您确实有另一个更严重的错误,那就是您在将节点e 释放deleteNode. 这是未定义的行为,是一件坏事。

于 2012-09-26T07:52:50.120 回答