1

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.

6
  • 2
    Have you tried free((*queue)->arr)? Commented Apr 1, 2015 at 17:09
  • 1
    What do you mean exactly? you are posting a compiler error and then you talk about valgrind? How, if you haven't got an executable? Btw. if should be free( (*queue)->arr ); Commented Apr 1, 2015 at 17:10
  • Actually, I was thinking you were a dereference short: free(*(**queue)->arr); Commented Apr 1, 2015 at 17:13
  • I just tried free((*queue)->arr) now and it works. Thanks! I still fail to understand why though. Commented Apr 1, 2015 at 17:13
  • because *queue->arr is equal to *(queue->arr). So if queue is queue_t ** you can't use -> on it but have do dereference it first; that's done by the bracketing Commented Apr 1, 2015 at 17:16

1 Answer 1

3

The free statement should look like free((*queue)->arr).

Explanation: queue_free defines the queue variable as a pointer to a pointer to the queue_t struct. Now, in order to get to the pointer to the struct you need to remove one level of indirection using (*queue); then you can use the dereference operator -> to access arr member.

Now, the reason why the other options didn't work for you are:

  • free(queue -> arr) - a pointer to a pointer of a queue_t structure does not have an arr member
  • free(*queue -> arr) - the dereference operator ('*') refers to the whole expression, i.e. queue->arr, which takes you back to the previous error
  • free(**queue -> arr) - same as above
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.