I can't figure out why my linked list has a memory leak.
I have a method called insertAtFront()
void insertAtFront(Node **head, char *key) {
Node *new = malloc(sizeof(Node));
if (!new) fatal("Malloc of new Node failed");
new->word = key;
new->next = *head;
*head = new;
}
Then I have a removeAtFront()
method that frees the node.
void removeAtFront(Node **head) {
Node *newhead = (*head)->next;
free(*head);
*head = newhead;
}
If I only add one node and then remove it, there is no leak. If I remove more than one node, valgrind shows a leak proportional to how many additional nodes were added. I really can't understand why this happens as the node is freed. Any ideas?