使用链表结构跟踪 C 程序中可能的内存泄漏
Tracking Possible Memory Leak in C program using Linked List Struct
该程序应该读取输入,直到读取到 0 或负数。然后使用下面的结构将其按升序放入链表中。
当我收到测试用例 0-3 的电子邮件时,我全部通过了。这些输入由随机数字组成,例如 1 2 3 4 5 -1 或 3493494 2922 -1.
最后一个输入文件“#### failed test”包含一个空白页,即输入 txt。所以我的程序自动说 "The linked list is empty"。但是出现内存检查错误。
这个错误是因为block吗? "free(curr);" 应该在 printf 之后吗?
if(head == NULL)
{
printf("The list is empty\n");
return(-1);
}
我问这个是因为当程序正常运行并执行时,它到达最后一行,它释放了 curr 变量的内存。但是当列表为空时,它会立即转到 "head == NULL" 语句并在不释放变量的情况下退出。
我的教授通过电子邮件向我发送了每个输入不同的测试用例:
#### 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==
代码:
#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);
}
您需要释放最后一个 while 循环中的 head
节点:
while(1)
{
if(head == NULL) {
printf("NULL\n");
break;
}
printf("%d-->", head->node);
void *tmp = head;
head = head->next;
free(tmp);
}
并删除 free(curr);
.
您还应该检查 scanf
是否成功,或者之前将 n
设置为某个负值。
该程序应该读取输入,直到读取到 0 或负数。然后使用下面的结构将其按升序放入链表中。
当我收到测试用例 0-3 的电子邮件时,我全部通过了。这些输入由随机数字组成,例如 1 2 3 4 5 -1 或 3493494 2922 -1.
最后一个输入文件“#### failed test”包含一个空白页,即输入 txt。所以我的程序自动说 "The linked list is empty"。但是出现内存检查错误。
这个错误是因为block吗? "free(curr);" 应该在 printf 之后吗?
if(head == NULL)
{
printf("The list is empty\n");
return(-1);
}
我问这个是因为当程序正常运行并执行时,它到达最后一行,它释放了 curr 变量的内存。但是当列表为空时,它会立即转到 "head == NULL" 语句并在不释放变量的情况下退出。
我的教授通过电子邮件向我发送了每个输入不同的测试用例:
#### 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==
代码:
#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);
}
您需要释放最后一个 while 循环中的 head
节点:
while(1)
{
if(head == NULL) {
printf("NULL\n");
break;
}
printf("%d-->", head->node);
void *tmp = head;
head = head->next;
free(tmp);
}
并删除 free(curr);
.
您还应该检查 scanf
是否成功,或者之前将 n
设置为某个负值。