我在 c 中創建了一個鏈表,我遇到了這個記憶體泄漏。Valgrind 說問題的根源在第 15 行,但我仍然看不到問題所在。我覺得很愚蠢,不知道問題出在哪里,但如果你能幫助我,那就太好了!提前致謝!
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct node
{
int number;
struct node *next;
}
node;
int main(void)
{
node *list = malloc(sizeof(node));
if (list == NULL)
{
return 1;
}
list = NULL;
// create link list
for (int i = 0; i < 3; i )
{
node *n = malloc(sizeof(node));
if(n == NULL)
{
free(list);
return 1;
}
n->number = i 1;
n->next = NULL;
n->next = list;
list = n;
}
//print link list
for (node *tmp = list; tmp != NULL; tmp = tmp->next)
{
printf("%d - ", tmp->number);
}
printf("\n");
//free link list
while (list != NULL)
{
node *tmp = list->next;
free(list);
list = tmp;
}
}
這是我的 valgrind 輸出:
==3895== Memcheck, a memory error detector
==3895== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3895== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==3895== Command: ./temp
==3895==
3 - 2 - 1 -
==3895==
==3895== HEAP SUMMARY:
==3895== in use at exit: 16 bytes in 1 blocks
==3895== total heap usage: 4 allocs, 3 frees, 64 bytes allocated
==3895==
==3895== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3895== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==3895== by 0x401168: main (temp.c:15)
==3895==
==3895== LEAK SUMMARY:
==3895== definitely lost: 16 bytes in 1 blocks
==3895== indirectly lost: 0 bytes in 0 blocks
==3895== possibly lost: 0 bytes in 0 blocks
==3895== still reachable: 0 bytes in 0 blocks
==3895== suppressed: 0 bytes in 0 blocks
==3895==
==3895== For lists of detected and suppressed errors, rerun with: -s
==3895== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
uj5u.com熱心網友回復:
指標只是一個存盤記憶體地址的變數。您使用 malloc 獲得了記憶體地址,malloc(sizeof(node))
但您在其中輸入了 nulllist = NULL
并且您丟失了地址。這不是你想要的嗎?
node** list;
size_t i;
list = malloc(sizeof(node*));
*list = NULL;
for (i = 0; i < 3; i )
{
node* n = malloc(sizeof(node));
n->number = i 1;
n->next = *list;
*list = n;
}
/* print node... */
/* release node... free(n) */
free(list);
list = NULL;
uj5u.com熱心網友回復:
很明顯,這種記憶體分配
node *list = malloc(sizeof(node));
if (list == NULL)
{
return 1;
}
list = NULL;
是多余的,并且會產生記憶體泄漏,因為在list
記憶體分配后重新分配了指標。結果,分配的記憶體地址丟失。
寫吧
node *list = NULL;
同樣在此代碼段中
n->number = i 1;
n->next = NULL;
n->next = list;
list = n;
這個說法
n->next = NULL;
是多余的。
在 for 回圈中
for (int i = 0; i < 3; i )
{
node *n = malloc(sizeof(node));
if(n == NULL)
{
free(list);
return 1;
}
//...
您需要釋放所有分配的記憶體。
例如
for (int i = 0; i < 3; i )
{
node *n = malloc(sizeof(node));
if(n == NULL)
{
while ( list )
{
node *tmp = list;
list = list->next;
free( tmp );
}
return 1;
}
//...
為避免代碼重復,您可以撰寫一個單獨的函式來釋放分配的記憶體。例如
void free_list( node **list )
{
while ( *list )
{
node *tmp = *list;
*list = ( *list )->next;
free( tmp );
}
}
并稱之為例如
for (int i = 0; i < 3; i )
{
node *n = malloc(sizeof(node));
if(n == NULL)
{
free_list( &list );
return 1;
}
//...
或者在程式結束時
free_list( &list );
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457193.html