I freed a pointer that I made, but Valgrind still reports that I’ve lost bytes, so I’m not really sure how to approach it.
The code in question Is:
listNode* temp = (listNode*)malloc(sizeof(listNode));
node = list->head;
while (node != NULL)
{
temp = node->next;
free(node->entry);
free(node);
node = temp;
}
free(temp);
With the valgrind output:
16 bytes in 1 blocks are definitely lost in loss record 13 of 21
==2137== at 0xB823: malloc (vg_replace_malloc.c:266)
==2137== by 0x100001B1E: freeList (main.c:110)
==2137== by 0x100001CB5: main (main.c:157)
You don’t
freethe memory allocated withmallocin the code given, unlessnode == NULL. You allocate a block of memory, assign it totemp, then go on in the loop to reassign totempother addresses, losing the memory that you’ve allocated withmalloc.It doesn’t look like you need the
mallocat all: you’re just usingtempas a temporary pointer: why do you need to allocate memory for it?