【问题标题】:Freeing Memory after Linked List Removal删除链表后释放内存
【发布时间】:2015-02-06 16:25:43
【问题描述】:

我在空闲时间学习 C。我熟悉 C#、Java 和 Python。作为练习,我用 C 语言编写了一个链表。它功能正确,具有错误处理等功能。

但是,我正在尝试修复内存泄漏。我知道 C 没有自动垃圾收集。那么,我如何在删除列表成员后“释放”它?我编写了一个名为 removeAllList() 的函数。该函数成功地从列表中删除了该成员,但我知道该成员的内存仍在分配中。我曾尝试使用 free([myArgument]) 函数,但它会导致无限循环。你能说明我会在哪里使用 free() 函数为我的代码中的已删除成员成功释放内存吗?

#include<stdio.h>
#include<stdlib.h>

struct Member{
    int data;
    struct Member *next;
};

struct List{
    int size;
    struct Member *root;
};

struct Member *createMember(int i){
    struct Member *new;

    new = malloc(sizeof(struct Member));
    new->data = i;
    new->next = NULL;
    return new;
}

struct List *createList(int i){
    struct List *new;

    new = malloc(sizeof(struct List));
    new->root = createMember(i);
    new->size = 1;
    return new;
}

void printList(struct List *list){
    struct Member *current = list->root;

    //error handling for empty list
    if(list->size < 1){
        printf("Error: List is empty");
    }
    //if list is not empty
    else{
        printf("List size: %i\nContents: ", list->size);
        while(current->next != NULL){
            printf("%i, ", current->data);
            current = current->next;
        }
        printf("%i\n", current->data);
    }
}

void addList(struct List *list, int i){
    struct Member *current = list->root;

    while(current->next != NULL){
        current = current->next;
    }
    current->next = createMember(i);
    list->size++;
}

void removeAllList(struct List *list, int i){
    struct Member *current = list->root;
    struct Member *prev = list->root;

    if(list->size < 1){
        //list is empty, end function now
        return;
    }

    //remove all matching list head
    while(current->data == i){
        if(list->size <= 1){
            list->root = NULL;
            list->size--;
            //list is empty, end function now
            return;
        }
        else{
            list->root = current->next;
            current = list->root;
            list->size--;
        }
    }
    current = current->next;

    //remove all matching list body
    while(current->next != NULL && list->size > 1){
        if(current->data == i){
            prev->next = current->next;
            list->size--;
        }
        prev = current;
        current = current->next;
    }

    //remove all matching list tail
        if(current->data == i && list->size > 1){
            prev->next = NULL;
            list->size--;
        }
}

main(){
        struct List *myList; 
        myList = createList(4);
        addList(myList, 12);
    addList(myList, 9);
    addList(myList, 4);
    addList(myList, 43);
    addList(myList, 4);
        printList(myList);
    removeAllList(myList, 4);
    printList(myList);
}

【问题讨论】:

  • 您是否尝试过man 3 freeman malloc 获取信息?

标签: c memory-management linked-list


【解决方案1】:

你不应该做消耗内存的东西NULL。你需要像这样free()他们:free(mynode-&gt;next);,但前提是你确定之前有一个malloc()调用。

在您的代码中,您需要使用free(list-&gt;root); 而不是list-&gt;root = NULL;free(prev-&gt;next); 而不是prev-&gt;next = NULL;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    相关资源
    最近更新 更多