m3k3r1 m3k3r1 - 1 year ago 98
C Question

Free a linked list in c

I'm trying to free these linked lists and i get no compile errors but valgrind is saying "30 allocs, 14 frees, 6,377 bytes allocated ".
Shouldn't this function wipe all the allocations in the lists.

void free_mem(struct medico *head_m, struct paciente *head_p ,
struct consulta *head_c)
struct medico *tmp_m = NULL;
struct paciente *tmp_p = NULL;
struct consulta *tmp_c = NULL;

while (!head_m)
tmp_m = head_m->next;
head_m = tmp_m;
while (!head_p)
tmp_p = head_p->next;
head_p = tmp_p;
while (!head_c)
tmp_c = head_c->next;
head_c = tmp_c;

Answer Source

Your loop conditions are wrong:

while (!head_m)

If head_m is not NULL, the loop is never executed. To fix, remove !:

while (head_m)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download