Another question regarding memory

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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

Re: Another question regarding memory

 
0
  #11
Mar 10th, 2005
Thanks, Narue and Stack Overflow (esp. for your memory allocation techniques).
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: Another question regarding memory

 
0
  #12
Mar 10th, 2005
Hello,

You're welcome. If you have any further questions regarding the information I provided, don't hesitate to ask.


- 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: Another question regarding memory

 
0
  #13
Mar 13th, 2005
Dear Stack Overflow,

I have a funny observation which I am not sure if it makes any sense. With regards to your code

  1. /* free rows made by MATRIXint() */
  2. /* where V represents the rows */
  3. for (i = V-1; i >= 0; i--) {
  4. if (G->adj[i] != NULL) {
  5. free(G->adj[i]);
  6. G->adj[i] = NULL;
  7. }
  8. }
  9.  
  10. /* free 2-dimensional pointer */
  11. if (G->adj != NULL) {
  12. free(G->adj)
  13. G->adj = NULL;
  14. }
  15.  
  16. /* free Graph */
  17. if (G != NULL) {
  18. free(G);
  19. G = NULL;
  20. }

it appears that when I use it to free up memory used for my graph G in the main program, it works, but when I write it as a function and include it as a header file as follows

  1. void freegraph(G)
  2. { int i;
  3. /* free rows made by MATRIXint() */
  4. /* where V represents the rows */
  5. for (i = (G->V)-1; i >= 0; i--) {
  6. if (G->adj[i] != NULL) {
  7. free(G->adj[i]);
  8. G->adj[i] = NULL;
  9. }
  10. }
  11.  
  12. /* free 2-dimensional pointer */
  13. if (G->adj != NULL) {
  14. free(G->adj)
  15. G->adj = NULL;
  16. }
  17.  
  18. /* free Graph */
  19. if (G != NULL) {
  20. free(G);
  21. G = NULL;
  22. }
it doesn't work. I passed my graph G to this function and then had the following code in my main function
  1. if(G==NULL)printf("success"); else printf("fail");
I got fail instead of the expected success. Why is this so?
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: Another question regarding memory

 
0
  #14
Mar 13th, 2005
I think I have just found the answer to my own question....thanks to all for looking!
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



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC