How to free up memory used for queues?

Thread Solved

Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

How to free up memory used for queues?

 
0
  #1
Mar 8th, 2005
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++];}
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: How to free up memory used for queues?

 
0
  #2
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #3
Mar 8th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,333
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to free up memory used for queues?

 
0
  #4
Mar 8th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #5
Mar 8th, 2005
thanks! help appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #6
Mar 9th, 2005
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;}
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #7
Mar 9th, 2005
Apologies for my previous code....the free function should be

  1. free(G);

I mistyped it. Any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: How to free up memory used for queues?

 
0
  #8
Mar 9th, 2005
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: How to free up memory used for queues?

 
0
  #9
Mar 9th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: How to free up memory used for queues?

 
0
  #10
Mar 9th, 2005
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC