943,492 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 7237
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 8th, 2005
0

How to free up memory used for queues?

Expand Post »
I have a first-in-first-out queue implementation such that when it is first initialized, a malloc is called to set aside memory for items that will be placed on the queue. When I am done with the queue, how do I free up the memory?
My code for the queue is as follows:

  1. typedef Edge Item;
  2. void QUEUEinit(int);
  3. int QUEUEempty(void);
  4. void QUEUEput(Item);
  5. Item QUEUEget(void);
  6.  
  7. static Item *q;
  8. static int N,head,tail;
  9.  
  10. void QUEUEinit(int maxN)
  11. {q = malloc((maxN+1)*sizeof(Item));
  12. N = maxN+1; head=N; tail=0;}
  13.  
  14. int QUEUEempty()
  15. { return head % N == tail;}
  16.  
  17. void QUEUEput(Item item)
  18. { q[tail++] = item;
  19. tail = tail % N;}
  20.  
  21. Item QUEUEget()
  22. { head = head % N;
  23. return q[head++];}
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 8th, 2005
0

Re: How to free up memory used for queues?

As defined in stdlib.h and malloc.h
  1. free (q);
will do the trick. Assure only pointers that were created with malloc, calloc or realloc are passed to free (), otherwise other calls to get memory may have unpredictable results.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Mar 8th, 2005
0

Re: How to free up memory used for queues?

I did try the free(q), but was surprised when the following thing happened:

when there were still items on my queue, i did a free(q) and then tried to access items on my queue, it turns out that the first remaining item was not accessible but the 2nd, 3rd item so on were accessible. any explanation for this?
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 8th, 2005
0

Re: How to free up memory used for queues?

Quote originally posted by kloony ...
I did try the free(q), but was surprised when the following thing happened:

when there were still items on my queue, i did a free(q) and then tried to access items on my queue, it turns out that the first remaining item was not accessible but the 2nd, 3rd item so on were accessible. any explanation for this?
Once you've freed it, consider all of it inaccessible. If it appears that items are still accessible, that's just a temporary illusion to make you think you're doing something you're allowed to.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 8th, 2005
0

Re: How to free up memory used for queues?

thanks! help appreciated.
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

The free() function is still puzzling me in some sense. I have a simple code below which creates a structure called graph. The code compiles without any problem and when it is executed, the output prints out 2 "10"'s, one before the free(), another one after. Did the memory set aside for G really got freed?

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. main()
  5. { typedef struct graph *Graph;
  6. struct graph{int V; int E;};
  7.  
  8. int V=10;
  9. int E=10;
  10.  
  11. Graph G=malloc(sizeof *G);
  12. G->V=V;
  13. G->E=E;
  14.  
  15. printf("%d\n", G->E);
  16. free(*G);
  17. printf("%d\n", G->E);
  18. return 0;}
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

Apologies for my previous code....the free function should be

  1. free(G);

I mistyped it. Any help would be greatly appreciated.
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

Hello,

When free() is called, a block of memory previously allocated by a call to malloc, calloc or realloc is freed:
Graph G=malloc(sizeof *G); /* Memory is dynamically allocated */
if (G != NULL) { /* G successfully points to an empty block of memory */
	/* Do task here */
}else if (G == NULL) {
	return 0; /* Memory allocation failed */
}

free(G); /* Dynamically allocated memory is freed */
Further documentation:

C Reference: malloc()
C Reference: free()


- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Mar 9th, 2005
0

Re: How to free up memory used for queues?

Thanks, that's why I thought that after I "free(G)", the code

  1. printf("%d\n", G->E);

should not be able to return me a value of 10. when it does, it surprises me, which makes me wonder whether the memory has really been freed.
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 9th, 2005
0

Re: How to free up memory used for queues?

Hello,

Also, be sure to set your variable to NULL after you free the memory. Example:
free(G); /* Dynamically allocated memory is freed */
G = NULL; /* This ensures that we point our pointer to NULL */
This can ensure a few things. Firstly, it ensures our pointer doesn't point to our freed address. Secondly, it secures your code from crashing if you call free() twice on the same pointer. For instance:
free(G); /* Dynamically allocated memory is freed */
free(G); /* Will cause a crash. */
Where in fact:
free(G); /* Dynamically allocated memory is freed */
G = NULL; /* This ensures that we point our pointer to NULL */
free(G); /* Won't cause a crash because G points to NULL. */
Hope this helps.


- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Adding to linked list from external file
Next Thread in C Forum Timeline: can someone help...please with this prb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC