LinkedList - 如何释放使用 malloc 分配的内存 [英] LinkedList - How to free the memory allocated using malloc

查看:33
本文介绍了LinkedList - 如何释放使用 malloc 分配的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的 C 代码来构造一个单链表,如下所示,其中我使用 malloc 为每个节点动态分配内存.在代码的最后,我想为每个分配的节点释放内存,想知道如何去做 - 如果我首先从头节点开始并释放它,那么指向后续节点的指针就会丢失并且会发生内存泄漏.

I have a very simple C code for constructing a Singly Linked list as below, in which I allocate memory for each node dynamically using malloc. At the end of code, I want to free the memory for each node allocated, was wondering how to go about it - If I start from head node first and free it, the pointers to the subsequent nodes are lost and memory leak happens.

另一种方法是从头节点开始,将节点指针继续存储在一个单独的指针数组中,在存储节点指针的同时遍历列表直到尾指针,一旦到达尾节点,也将其存储到其他指针数组,并从该数组索引向后开始释放,直到头节点被释放.

Other way is start from head node and keep storing the node pointer in a separate array of pointers or something, traverse the list till the tail pointer while storing the node pointers, and once reach the tail node, store that also to the other array of pointers and start freeing from that array index backwards until the head node is free'ed.

这是实现我想要做的事情的唯一方法吗?

Is that the only way to achieve what I am trying to do?

如果我不想使用第二个缓冲区,我该怎么做.

In case if I dont want to use second buffer, how do I go about it.

#include "stdio.h"
#include "stdlib.h"

struct lnk_lst 
{
   int val;
   struct lnk_lst * next;
};

typedef struct lnk_lst item;


main()
{
   item * curr, * head;
   int i,desired_value;

   head = NULL;

   for(i=1;i<=10;i++) 
   {
      curr = (item *)malloc(sizeof(item));
      curr->val = i;
      curr->next  = head;
      head = curr;
   }

   curr = head;


   while(curr) {
      printf("%d
", curr->val);
      curr = curr->next;
   }

  //How to free the memory for the nodes in this list?
   for(i=1;i<=10;i++)
   {
       free()//?? What logic here
   }


}

推荐答案

通常的方式是用(伪代码在前):

The usual way is with (pseudo-code first):

node = head              # start at the head.
while node != null:      # traverse entire list.
    temp = node          # save node pointer.
    node = node.next     # advance to next.
    free temp            # free the saved one.
head = null              # finally, mark as empty list.

基本思想是在一个单独的变量中记住要释放的节点,然后在释放它之前前进到下一个.

The basic idea is to remember the node to free in a separate variable then advance to the next before freeing it.

您一次只需要记住一个节点,而不是您建议的整个列表.

You only need to remember one node at a time, not the entire list as you propose.

就您需要添加到代码中的内容而言,您可以在删除过程中使用 head 作为不断更新的列表头(正如它本来的样子)和 curr 存储您当前正在删除的项目:

In terms of what you need to add to your code, you can, during deletion, use head as the continuously updating list head (as it's meant to be) and curr to store the item you're currently deleting:

while ((curr = head) != NULL) { // set curr to head, stop if list empty.
    head = head->next;          // advance head to next element.
    free (curr);                // delete saved pointer.
}

这比上面的伪代码要短一些,只是因为它在某些操作中利用了 C 的速记".

This is a little shorter than the pseudo-code above simply because it takes advantage of C "shorthand" for some operations.

这篇关于LinkedList - 如何释放使用 malloc 分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆