Thread: Memory Management

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    Memory Management

    I am really new to C++. All my programming experience is in Java. I'm still learning about memory management. I'm trying to put together a BST. Everything works except that my clear method doesn't work yet. It is supposed to delete all the nodes of the tree. I also want it to free up the memory that the nodes used too. So the code I used was this:

    Code:
    void BST::deleteAll(BSTNode* current)
    	{
    		if(current->left!=NULL)
    			deleteAll(current->left);
    		if(current->right!=NULL)
    			deleteAll(current->right);
    		
    		delete current;		
    	}
    When I call clear I pass the root of my tree into this deleteAll function. The problem is that my root node seems to still be there. It's not null or anything like I'd expect. Any thoughts?

    Thanks for the help you guys!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    delete doesn't set anything to NULL. The pointer isn't valid, true, but so what. You should set head = NULL after you finish. (Note well that you can't set current = NULL in this function, as that change won't be seen in the calling function.)

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Now on that code I am getting a seg fault. My debug comes back with:

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x0806779d in BST::deleteAll (this=0xbff1e6a8, current=0x568b000f) at BST.cpp:175
    175			if(current->left!=NULL)
    What am I doing wrong here?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Very difficult to say without code to test.
    If I had to guess, I'd say you're using a pointer that's freed.
    You should realize that you can also use smart pointers, such as std::tr1::shared_ptr (alternatively boost::shared_ptr). They will take care of freeing the memory automatically.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Wow, now I feel really dumb. Someone wanna give me a crash course in smart pointers? That's the first time I've even heard of them.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You aren't alone in that.
    http://msdn.microsoft.com/en-us/library/bb982026.aspx
    http://msdn.microsoft.com/en-us/libr...83(VS.80).aspx
    http://msdn.microsoft.com/en-us/library/bb982126.aspx
    They're a recent addition to the C++ standard library and a pretty recent design implementation I think, but future C++ will heavily build upon these smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Thanks for the help everyone!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    They're a recent addition to the C++ standard library and a pretty recent design implementation I think, but future C++ will heavily build upon these smart pointers.
    In fact, they (aside from std::auto_ptr) are not even part of the C++ standard library, yet.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would assume you're creating this BST for learning purposes, right? In that case, maybe a smart pointer for this particular exercise doesn't make sense. However, I'd certainly recommend you learn them for general C++ programming.

    If you pass the current pointer by reference and set current = 0 after the delete current call then the pointers will all be set to null and your code might work better.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    In fact, they (aside from std::auto_ptr) are not even part of the C++ standard library, yet.
    Well, I suppose that technically they aren't, but they're in the TR1 library which has been finalized and is moving into the standard library soon enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM