简体   繁体   English

使用链接列表结构跟踪C程序中可能的内存泄漏

[英]Tracking Possible Memory Leak in C program using Linked List Struct

This program is supposed to read inputs until 0 or a negative number is read. 该程序应该读取输入,直到读取0或负数为止。 Then put it into a linked list in ascending order using the structure below. 然后使用以下结构按升序将其放入链接列表。

When I was emailed the test cases 0-3 I passed them all. 当我收到测试用例0-3的电子邮件时,我全部通过了测试。 Those inputs consist of numbers that are random such as 1 2 3 4 5 -1 or 3493494 2922 -1. 这些输入由随机数组成,例如1 2 3 4 5 -1或3493494 2922 -1。

The last input file which is "#### failed test" consists of a blank page which is the input txt. 最后一个输入文件为“ ####测试失败”,由空白页(即输入txt)组成。 So my program automatically says "The linked list is empty". 因此,我的程序会自动显示“链接列表为空”。 However there is a memory check error. 但是,存在内存检查错误。

Is this error because of the block? 是因为块导致此错误吗? Should "free(curr);" 应该“免费(发生);” be after the printf? 在printf之后?

  if(head == NULL)
  {
     printf("The list is empty\n");
     return(-1);
  }

I am asking this because when the program runs normally and executes it reaches the last line which free's the memory for the curr variable. 我之所以这样问是因为,当程序正常运行并执行时,它到达最后一行,这为curr变量释放了内存。 But when the list is empty it instantly goes to the "head == NULL" statement and exits without freeing the variable. 但是,当列表为空时,它立即转到“ head == NULL”语句,并在不释放变量的情况下退出。

My Professor emailed me the test cases where the input is different for each: 我的教授通过电子邮件向我发送了测试案例,每个案例的输入都不相同:

 #### Passed test 0.
 #### Passed test 1.
 #### Passed test 2.
 #### Passed test 3.
 #### Failed test MemoryCheck. Output of memory checker follows.
 ==23571== 128 (32 direct, 96 indirect) bytes in 2 blocks are definitely        
 lost in loss record 2 of 2
 ==23571== at 0x4A07218: malloc (vg_replace_malloc.c:296)
 ==23571== by 0x40071A: main (in /eecs/dept/course/2015-    
 16/F/2031/submit/lab7/cse13020/q2) 
 ==23571== 

Code: 码:

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


struct node
{
   int node;
   struct node* next;
};


void insert(struct node** head, struct node* node)
{
    struct node* curr;

    if (*head == NULL || (*head)->node >= node->node)
    {
       node->next = *head;
       *head = node;
    }
    else
    {
       curr = *head;

        while (curr->next!= NULL && curr->next->node < node->node)
           curr = curr->next;

        node->next = curr->next;
        curr->next = node;
    }
}


int main()
{

   struct node *head = NULL;
   struct node *curr;

   int n;

   while(1)
   {

      scanf("%d", &n);

      if(n <= 0) 
        break;

      curr = (struct node*) malloc(sizeof(struct node));
      curr->node  = n;
      curr->next =  NULL;

      insert(&head, curr); 
   }

  if(head == NULL)
  {
     printf("The list is empty\n");
     return(-1);
  }

  printf("The linked list is:\n");

  while(1)
  {
     if(head == NULL) {
       printf("NULL\n");
       break;
     }
     printf("%d-->", head->node);
     head = head->next;
  }


  free(curr);  
}

you need to free the head node in the last while loop: 您需要在最后一个while循环中释放head节点:

while(1)
{
  if(head == NULL) {
    printf("NULL\n");
    break;
  }
  printf("%d-->", head->node);
  void *tmp = head;
  head = head->next;
  free(tmp);
}

and remove the free(curr); 并删除free(curr); .

You should also check scanf for success or set n to some negative value before. 您还应该检查scanf是否成功,或者之前将n设置为某个负值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM