Your loop is a bit awkward, and the function always returns true
if N > 0
. Yet your code seems fine and frees all the nodes. The memory leak is elsewhere: do the nodes have pointers to allocated memory (besides next
)?
Here is a simpler version:
int free_table(node *table, int N) {
for (int i = 0; i < N; i++) {
node *cursor = table[i]; // use cursor to enumerate the bucket list
table[i] = NULL; // optional
while (cursor != NULL) {
node *next = cursor->next; // save the next pointer
free(cursor);
cursor = next;
}
}
return N > 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…