943,695 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4725
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 10th, 2005
0

Re: Another question regarding memory

Thanks, Narue and Stack Overflow (esp. for your memory allocation techniques).
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 10th, 2005
0

Re: Another question regarding memory

Hello,

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


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

Re: Another question regarding memory

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?
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 13th, 2005
0

Re: Another question regarding memory

I think I have just found the answer to my own question....thanks to all for looking!
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005

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: strange problem
Next Thread in C Forum Timeline: problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC