Ich habe ein Problem, den Speicher in einer einfachen verketteten Liste Implementierung in C. freigegeben. Valgrind sagt mir, ich habe nicht alles befreit, aber ich bin nicht in der Lage herauszufinden, wo das Problem Lügen. Mein Code und valgrind Ausgabe unter:Freigeben von Speicher in einfache verkettete Liste - 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 zeigt folgende Fehler
==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)
Erneutes Kompilieren und erneutes Verknüpfen mit der Option "-g" - dann erhalten Sie Zeilennummern, die Ihnen helfen. Sie legen 'item-> next 'nicht in der' insert() '-Funktion fest, was wahrscheinlich für die bedingten Sprung- oder Verschiebungsfehler verantwortlich ist - ebenso wie das Problem der doppelten Zuweisung, das ebenfalls identifiziert wurde. –
@JonathanLeffler danke für die -g-Flagge, ich fragte mich, warum ich keine Zeilennummern in meiner Valgrind-Ausgabe bekam. – CatsLoveJazz