Let's say I am implementing a Linked List and I have the following structures:
I also have various functions including:
Now, in both createNode() and LinkedList_init() I use malloc() to allocate the memory needed by the structure therefore I need to free this memory when I no longer have any need for the structures.
The Question:
If I initialise a linked list ( LinkedList *list = LinkedList_init(); ) how would I free the memory? Would I need to invoke destroyNode( list->head ) or is it simply free( list ) ?
Thanks!
Code:
typedef struct Node
{
void *element;
struct Node *nextNode;
} Node;
typedef struct LinkedList
{
Node *head;
} LinkedList;
I also have various functions including:
Code:
Node *createNode( void *element, Node *nextNode );
void destroyNode( Node *node );
LinkedList *LinkedList_init( void );
Now, in both createNode() and LinkedList_init() I use malloc() to allocate the memory needed by the structure therefore I need to free this memory when I no longer have any need for the structures.
The Question:
If I initialise a linked list ( LinkedList *list = LinkedList_init(); ) how would I free the memory? Would I need to invoke destroyNode( list->head ) or is it simply free( list ) ?
Code:
void destroyLinkedList( LinkedList *list )
{
destroyNode( list->head );
free( list );
}
or
void destroyLinkedList( LinkedList *list )
{
free( list );
}
Thanks!