I’m currently writing a linked list and trying to free up memory allocations when I delete a node. However, after hours of trying to do this, I can’t seem to get a clean valgrind output.
void * pop(struct List *list)
{
if(list->head == 0){
return 0;
}
struct Node * tempNode = list->head->next;
free(list->head);
list->head = tempNode;
...
}
I’m allocating the space by saying:
addNode(struct List *list, void *element){
struct Node *node;
node = (struct Node *)malloc(sizeof(node));
....
}
Basically in the pop function I want to take out the head of the list and make the head’s next node the new head. I want to deallocate the memory that was given to head.
Thanks for any help
Woah, your
mallocisn’t correct. You have:What you need is:
In your original code, you are only allocating enough for a pointer. But you are trying allocate a
Nodeobject.