Hi all,
So, in general, freeing a linked list is pretty straight forward. my freeing function looks like this:
However I want to optimise my code some, and don't want to have separate malloc calls for every node in a given list. However, the way the program runs, I may have a couple separate nodes followed by a bunch of nodes that are stored in a contiguous memory block that was malloced all at once. The problem is, of course, that the free_chain function will try to free each node individually, even if they were malloced as one chunk, and thus run into a double free problem. How do I get around this?Code:typedef struct node { uint64_t digit; struct node *next; struct node *prev; } node; void free_chain(node* hostage) { node* tmp_pntr; while (hostage) { tmp_pntr=hostage->next; free(hostage); //(!) hostage=tmp_pntr; } return; }
Thanks so much!