CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Deallocating space used by our queue

It's considered a good practice to always write a function that deallocates all space used in our own user-defined dynamic data structures.

In the case of our queue, we need to deallocate 3 things:

  1. the memory required for the data in every element,
  2. the memory required for every element,
  3. the queue itself.

void queue_free(QUEUE *q)
{
    ELEMENT     *this, *save;

    this  = q->head;
    while( this != NULL ) {
        save      = this;
        this      = this->next;
        free(save->data);
        free(save);
    }
    free(q);
}

    QUEUE  *my_queue  =  queue_new();
    ....
    //  use my local queue
    ....
    queue_free( my_queue );

 


CITS2002 Systems Programming, Lecture 19, p15, 3rd October 2023.