在C中释放内存的困惑?

Col*_*ght 1 c malloc free gdb linked-list

关于我在这里做错了什么,我感到很困惑.我有一个链接的节点列表(见下面的结构),我很困惑如何在修剪列表时释放内存.我认为将它们添加到数组(eventArr)会使它们以后更容易释放,但我仍然遇到问题!谁能帮助我指出正确的方向?

static void trim_list(int room, int keep, char timestamp[]) {

        struct room_t *the_room;
        struct room_t *the_room_copy;

        struct evnode_t *eventArr[20];
        int index = 0;

        the_room_copy = malloc(sizeof(struct room_t));
        the_room = find_room(room);

        the_room_copy->room = the_room->room;
        the_room_copy->ecount = the_room->ecount;
        the_room_copy->evl_head = the_room->evl_head;

        struct evnode_t *node;

        int counter;
        int removed = 0;

        for(counter = 0; counter != keep; counter++){
          the_room_copy->evl_head = the_room_copy->evl_head->next;                 
        }

        while(the_room_copy->evl_head){
          eventArr[index] = the_room_copy->evl_head;
          the_room_copy->evl_head = the_room_copy->evl_head->next;
          index++;
          removed++;
        }

        for(index = 0; index < removed; index++){
          free(eventArr[index]);
        }

        printf("Trim log @ %s: room %i log trimmed to length %i (%i entries removed)\n",timestamp, room, keep, removed);

        return ;
}
Run Code Online (Sandbox Code Playgroud)

结构:

struct evnode_t {
        struct event_t event ;
        struct evnode_t *next ;
} ;

struct room_t {
        int room ;
        int ecount ;
        struct evnode_t *evl_head ;
        struct room_t *next_room ;
} ;

struct event_t {
        int sensor ;
        char time_stamp[MAX_TIMESTRING+1];
        int event_id ;
        int event_info ;
} ;
Run Code Online (Sandbox Code Playgroud)

我应该注意,我通过gdb运行这个以便更好地理解它,但由于某种原因,这些值变得混乱了.这是我的输出:

在运行trim_list之前......

(gdb) p room_list->room
$1 = 1
(gdb) p room_list->evl_head->event.sensor
$2 = 9
(gdb) p room_list->evl_head->next->event.sensor
$3 = 4
(gdb) p room_list->evl_head->next->next->event.sensor
$4 = 2
(gdb) p room_list->evl_head->next->next->next->event.sensor
$5 = 2
(gdb) p room_list->evl_head->next->next->next->next->event.sensor
$6 = 3
(gdb) p room_list->evl_head->next->next->next->next->next->event.sensor
$7 = 2
Run Code Online (Sandbox Code Playgroud)

运行trim_list后......

(gdb) p room_list->evl_head->next->event.sensor
$8 = 4
(gdb) p room_list->evl_head->next->next->event.sensor
$9 = 2
(gdb) p room_list->evl_head->next->next->next->event.sensor
$10 = 2
(gdb) p room_list->evl_head->next->next->next->next->event.sensor
$11 = 0
(gdb) p room_list->evl_head->next->next->next->next->next->event.sensor
$12 = 6396880
Run Code Online (Sandbox Code Playgroud)

任何关于我为什么会遇到这些问题的见解都会非常有用!我不确定我是否正确理解如何释放价值......但我确实知道所释放的价值都已被malloc删除.澄清,room_list是我试图从中释放值的列表,并且the_room_copy是新malloc版本的room_list.

JS1*_*JS1 5

看起来你正确地释放了节点.您没有做的是终止新位置的列表.例如,如果旧列表是:

1 -> 9 -> 4 -> 2 -> 2 -> 3 -> 2 -> NULL
Run Code Online (Sandbox Code Playgroud)

你想在第5个元素之后修剪,你现在正在做的事情就像这样:

1 -> 9 -> 4 -> 2 -> 2 -> (freed memory) -> (unknown pointer) -> ...
Run Code Online (Sandbox Code Playgroud)

您需要做的是在要保留的链接列表之后终止链接列表:

1 -> 9 -> 4 -> 2 -> 2 -> NULL
Run Code Online (Sandbox Code Playgroud)

以下是一些示例代码,显示了终止列表的一种方法:

    struct evnode_t *end_of_list = NULL;

    // This is your existing loop to skip past "keep" elements.
    for(counter = 0; counter != keep; counter++){
      end_of_list = the_room_copy->evl_head;
      the_room_copy->evl_head = the_room_copy->evl_head->next;                 
    }

    // Free stuff as you did before...

    // At the very end:
    if (end_of_list == NULL) {
        // Keep nothing.
        the_room->evl_head = NULL;
    } else {
        // Terminate the list at the proper place.
        end_of_list->next = NULL;
    }

    // Oh, you need to free the copy also, it's a memory leak:
    free(the_room_copy);
Run Code Online (Sandbox Code Playgroud)