C中释放内存的困惑



我完全弄不清楚我做错了什么。我有一个节点的链接列表(结构见下文),在修剪列表时,我对如何释放内存感到困惑。我原以为将它们添加到数组(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 ;
}

结构:

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 ;
} ;

我应该注意,为了更好地理解它,我在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

运行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

任何关于我为什么会遇到这些问题的见解都会很有帮助!我不确定我是否正确理解了如何释放价值观。。。然而,我知道被释放的值都是malloced。为了澄清,room_list是我试图从中释放值的列表,而the_room_copyroom_list的新malloced版本。

看起来您正在正确释放节点。您所不做的是在新位置终止列表。例如,如果旧列表为:

1 -> 9 -> 4 -> 2 -> 2 -> 3 -> 2 -> NULL

如果你想在第五个元素之后进行修剪,你现在所做的结果是这样的:

1 -> 9 -> 4 -> 2 -> 2 -> (freed memory) -> (unknown pointer) -> ...

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

1 -> 9 -> 4 -> 2 -> 2 -> NULL

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

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);

添加到Mr.JS1的答案中,要终止链表,您需要添加类似以下的代码:

struct evnode_t *node_to_terminate = the_room->evl_head;
for (int i = 1; i < keep; ++i)
{
node_to_terminate = node_to_terminate->evl_head;
}
node_terminate->next = NULL;

相关内容

  • 没有找到相关文章

最新更新