C Question

Associate
Joined
1 Mar 2010
Posts
1,389
Let's say I am implementing a Linked List and I have the following structures:

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!
 
Soldato
Joined
29 Jul 2004
Posts
9,617
Location
Somerset
I would *guess* that freeing list will only delete the pointer, not the linked data. You'd need a delete function really much like a destructor in c++.

Just a guess though.
 
Associate
OP
Joined
1 Mar 2010
Posts
1,389
I would *guess* that freeing list will only delete the pointer, not the linked data.

Ah, I see what you mean. I am only de-allocating the head node therefore the remaining list nodes are just sitting there taking up memory and being useless.

Edit: Something like this?

Code:
void destroyLinkedList( LinkedList *list )
{
    Node *currentNode = list->head;
    Node *temp;

    while ( currentNode != NULL )
    {
        temp = currentNode->nextNode;
        destroyNode( currentNode );
        currentNode = temp;
    }

    free( list );
}
 
Last edited:
Soldato
Joined
29 Jul 2004
Posts
9,617
Location
Somerset
Ah, I see what you mean. I am only de-allocating the head node therefore the remaining list nodes are just sitting there taking up memory and being useless.

I think so yes, it's kind of similar to....

Code:
 Fred* p = new Fred[100];
 ...
 delete p;
Just deletes the pointer at the start of the array, you need to use a delete[]. Fortunately C++ has std::list and vector etc. for fancier data structures.

Presumably though your destroyNode removes the pointer and free's the memory, so you just need to use this on all your list.

This could be blind leading the blind though :p. Someone who knows what they're talking about will turn up soon hopefully.
 
Last edited:
Associate
Joined
9 Jun 2004
Posts
423
Edit: Something like this?

Code:
void destroyLinkedList( LinkedList *list )
{
    Node *currentNode = list->head;
    Node *temp;

    while ( currentNode != NULL )
    {
        temp = currentNode->nextNode;
        destroyNode( currentNode );
        currentNode = temp;
    }

    free( list );
}

Yep, exactly, assuming destroyNode(node) frees node->element and then node.
Technically you should check if your list is null too ;)
 
Top Bottom