for an assignment I have to write a circular queue (which I imagine most of you are familiar with). I made a structure and a function that initializes a queue struct using dynamic memory. Note that I also have an array arr within the struct to which I assign memory.
For some reason I can't seem to free my array from memory.
struct queue { element_t *arr; // dynamic array containing data elements int current_size; // Counts number of elements in the queue int front, rear; // Remark for later: extra fields need to be added here to make it a thread-safe queue as needed for the assigment }; queue_t* queue_create(){ struct queue *queue = malloc(sizeof(struct queue)); if(queue == 0){ //check if memory available printf("Out of memory\n"); return NULL; } queue -> current_size = 0; queue -> front = -1; queue -> rear = -1; queue -> arr = malloc(QUEUE_SIZE*sizeof(element_t)); return queue; } I'm trying to free the memory when I'm done with it using a function queue_free. The function takes in a double pointer to a queue (part of the assignment).
void queue_free(queue_t** queue){ free(**queue -> arr); free(*queue); } When I call the last function I got an error message saying:
queue.c: In function ‘queue_free’: queue.c:39:16: error: request for member ‘arr’ in something not a structure or union free(**queue -> arr); ^ I've already checked with valgrind for leaks. The queue gets freed correctly but for some reason I can't get the address of my array.
Thanks in advance
P.S. free(queue -> arr); free(*queue -> arr); and free(**queue -> arr); all fail and generate the same error message.
free((*queue)->arr)?free( (*queue)->arr );*queue->arris equal to*(queue->arr). So ifqueueisqueue_t **you can't use->on it but have do dereference it first; that's done by the bracketing