У меня проблема с освобождением памяти, выделенной в простой реализации связанного списка. C. Valgrind говорит мне, что я не освободил все, но я не могу понять, где проблема вранье. Мой код и выход Valgrind ниже:Освобождение памяти, выделенной в простом связанном списке - C
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *next;
} node_t;
void insert(node_t *head, int num) {
// Create new node to insert
node_t *item = malloc(sizeof(node_t));
item = malloc(sizeof(node_t));
if (item == NULL) {
exit(2);
}
item->value = num;
// Insert new node at end of list
node_t *cur = head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = item;
}
int main(int argc, char *argv[]) {
// Create head or root node
node_t *head;
head = malloc(sizeof(node_t));
head->value = 0;
head->next = NULL;
// Insert nodes to end of list
insert(head, 1);
// Traverse list and print out all node values
node_t *cur = head;
while (cur != NULL) {
printf("%d\n", cur->value);
cur = cur->next;
}
// Free the list
cur = head;
node_t *previous;
while (cur != NULL) {
previous = cur;
cur = cur->next;
free(previous);
}
return 0;
}
// EOF
Valgrid показывает следующую ошибку
==9054== Memcheck, a memory error detector
==9054== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9054== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9054== Command: ./linkedlist
==9054==
0
1
==9054== Conditional jump or move depends on uninitialised value(s)
==9054== at 0x100000ED0: main (in ./linkedlist)
==9054== Uninitialised value was created by a heap allocation
==9054== at 0x100008EBB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==9054== by 0x100000E03: insert (in ./linkedlist)
==9054== by 0x100000EBF: main (in ./linkedlist)
==9054==
==9054== Conditional jump or move depends on uninitialised value(s)
==9054== at 0x100000F0E: main (in ./linkedlist)
==9054== Uninitialised value was created by a heap allocation
==9054== at 0x100008EBB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==9054== by 0x100000E03: insert (in ./linkedlist)
==9054== by 0x100000EBF: main (in ./linkedlist)
==9054==
==9054==
==9054== HEAP SUMMARY:
==9054== in use at exit: 26,456 bytes in 193 blocks
==9054== total heap usage: 274 allocs, 81 frees, 32,608 bytes allocated
==9054==
==9054== 16 bytes in 1 blocks are definitely lost in loss record 5 of 65
==9054== at 0x100008EBB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==9054== by 0x100000DF0: insert (in ./linkedlist)
==9054== by 0x100000EBF: main (in ./linkedlist)
==9054==
==9054== LEAK SUMMARY:
==9054== definitely lost: 16 bytes in 1 blocks
==9054== indirectly lost: 0 bytes in 0 blocks
==9054== possibly lost: 0 bytes in 0 blocks
==9054== still reachable: 0 bytes in 0 blocks
==9054== suppressed: 26,440 bytes in 192 blocks
==9054==
==9054== For counts of detected and suppressed errors, rerun with: -v
==9054== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 18 from 18)
Перекомпилируйте и повторно подключите опцию '-g' - тогда вы получите номера строк, которые помогут вам. Вы не устанавливаете 'item-> next' в функции' insert() ', которая, вероятно, учитывает условный переход или ошибки перемещения, а также проблему с двойным распределением. –
@JonathanLeffler благодарит вас за флаг -g, я задавался вопросом, почему я не получал номера строк в моем выходе valgrind. – CatsLoveJazz