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

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

问题描述

我有一个非常简单的C code构建一个单向链表下面,我在其中动态地使用malloc为每个节点分配内存。在code的结束,我想免费为分配每个节点的记忆,不知道如何去了解它 - 如果我从头节点开始第一次和免费的吧,指向后续节点都将丢失,内存泄漏发生。

另一种方法是从头部节点启动,并保持存储节点指针的指针或什么的一个单独的数组,遍历列表,直到尾指针,而存储节点的指针,而一旦到达尾节点,商店,还向指针的其它阵列,并开始从向后直到头节点free'ed该数组索引释放。

这是唯一的方式来实现我所试图做的?

在情况下,如果我不想使用第二个缓冲,我怎么去了解它。

 的#includestdio.h中
#包括stdlib.h中结构lnk_lst
{
   INT VAL;
   结构lnk_lst *接下来的;
};typedef结构lnk_lst项目;
主要()
{
   项目* CURR,*头;
   INT I,desired_value;   头= NULL;   对于(i = 1; I< = 10;我++)
   {
      CURR =(项目*)malloc的(sizeof的(项目));
      curr-> VAL = I;
      curr->接下来=头;
      头= CURR;
   }   CURR =头;
   而(CURR){
      的printf(%d个\\ N,curr-> VAL);
      CURR = curr->接下来,
   }  //如何释放在这个列表中的节点内存?
   对于(i = 1; I< = 10;我++)
   {
       自由()//??这里的逻辑是什么
   }
}


解决方案

通常的方法是用(伪code在前):

 节点=#头在先声夺人。
而节点=空:#!遍历整个列表。
    TEMP =节点#保存节点指针。
    节点= node.next#前进到下一个。
    免费临时#释放保存之一。
头= NULL#最后,标记为空列表。

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

您只需要记住的有一个的在一个时间节点,而不是整个列表,你建议。

在你需要添加到您的code什么条件,你可以,删除期间,使用作为不断更新列表头(因为它的意思定)和 CURR 来存储您当前删除的项目:

 ,而((CURR =头)!= NULL){//设置CURR头,停止,如果列表为空。
    头=流浆>接下来, //提前前往下一个元素。
    免费(CURR); //删除保存的指针。
}

这是不是伪code以上,只是因为它充分利用了C速记对某些操作略短。

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\n", 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.

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

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天全站免登陆