双向链表释放内存valgrind错误

时间:2018-09-07 07:38:09

标签: c pointers memory-leaks valgrind doubly-linked-list

嗨,我正在尝试释放在双向链表中分配的内存,但是当我用valgrind检查它时,free_all函数出现了一些错误(我认为),但是我不知道如何避免它

我认为在free_all函数中,我使用了temp和node指针错误,或者我需要先分配它们,然后再使用它们,但是当我尝试这种方法时,valgrind仍然给我一些错误。

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

/*
  to compile it:
  gcc -g -Wall -ggdb3  double_linkedlist2.c -o double_linkedlist
  to check for memory leak and error:
  valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind-out.txt ./double_linkedlist
*/
typedef struct listitem
{
  struct listitem *next;    // pointer to next item
  struct listitem *prev;    // pointer to previous item
  int data;                 // some data
} ITEM;


int main (void)
{
  // prototype functions
  void free_all (ITEM *lst_ptr);


  // Variables
  ITEM *p_temp, *head;

  head = malloc (sizeof (ITEM));  // head will keep first and last element in its pointers
  head -> next = head;            // the last element in the list (at first head -> next and head -> prev will point to the head)
  head -> prev = head;            // the first element in the list


     for (int i = 0; i < 3; i++)
       {
         p_temp = malloc (sizeof (ITEM));     // allocate some memory for the new list item
         p_temp -> data = i;                  // set the list item's data to the loop count so that we can see where it is in the list
         p_temp -> next = head -> next;          // this will insert at the FRONT of the list
         head -> next = p_temp;                  // and set the list head to the newly created list item
         p_temp -> prev = head;              // this will insert at the BACK of the list
         p_temp -> next -> prev = p_temp;       // and set the list 'tail' to the newly created item
       }

     // now let's see what we got going forward
     printf ("Going forward\n");
     p_temp = head -> next;

     while (p_temp != head)
       {
         printf ("forward list item: current is %p; next is %p; prev is %p; data is %d\n", p_temp, p_temp -> next, p_temp -> prev, p_temp -> data);
         p_temp = p_temp -> next;
       }

     // now let's see what we got going backward
     printf ("Going backwards\n");
     p_temp = head -> prev;

     while (p_temp != head)
       {
         printf ("backward list item; current is %p; next is %p; prev is %p; data is %d\n", p_temp, p_temp -> next, p_temp -> prev, p_temp -> data);
         p_temp = p_temp -> prev;
       }

     printf ("\n");
     free_all (head);

     return 0;
}

void free_all (ITEM *head)
{
  ITEM *temp, *node;

  node = head;

  while (node != head -> prev)
    {
      temp = node;
      printf ("freed list item: current is %p; next is %p; prev is %p; data is %d\n", temp, temp -> next, temp -> prev, temp -> data);
      node = node -> next;
      free (temp);
    }
  free (node);
  free (head);
}

3 个答案:

答案 0 :(得分:1)

您的free_all至少有两个错误: while条件引用head-> prev,但是在第一个迭代中,您释放了head(释放后使用)。 退出循环后,尽管在第一次迭代中释放了头部,您仍然可以释放头部。 free_all()适用于单元素情况。

答案 1 :(得分:0)

此修改后,valgrind中没有错误或内存泄漏

void free_all (ITEM *head)
{
  ITEM *temp, *node = NULL;

  node = head -> next;

  while (node != head -> prev)
    {
      temp = node;
      printf ("freed list item: current is %p; next is %p; prev is %p; data is %d\n", node, node -> next, node -> prev, node -> data);
      node = node -> next;
      free (temp);
    }

  node = head -> prev;
  printf ("freed list item: current is %p; next is %p; prev is %p; data is %d\n", node, node -> next, node -> prev, node -> data);
  free (head);
  free (node);
}

答案 2 :(得分:0)

这篇文章是由mevets写的,是对我的解决方案的编辑,但我认为最好也将其包含在线程中:

装饰品:

我倾向于:

void Unlink(ITEM **head, ITEM *t) {
        if (t->next == t) {
                /* remove head */
                *head = NULL;
        } else {
                t->prev->next = t->next;
                t->next->prev = t->prev;
                if (t == *head) {
                        *head = t->next;
                }
        }
}

/*
   remove and return the element after head
*/
ITEM *Pop(ITEM **head) {
        ITEM *node;
        if ((node = *head) != NULL) {
                node = node->next;
                Unlink(head, node);
        }
        return node;
}


void free_all (ITEM *head) {
        ITEM *node;
        while ((node = Pop(&head)) != NULL) {
            free(node);
        }
}

将列表维护(取消链接)与订购(Pop)和内存管理(free_all)分开。这给您留下了更多的边界,您可以在其中对列表进行断言,例如在取消链接之前和之后,列表应该是有效的并且可以检查。另外,如果可以同时访问列表,则可以将Pop()放在方括号内,以最大程度地减少冲突。

缓存分配器本身就是一件事,但是合同的一个典型部分是您释放处于已知状态的节点,以便在重新分配节点时可以跳过它们的构造。