Memory Management

This is a discussion on Memory Management within the C++ Programming forums, part of the General Programming Boards category; I am really new to C++. All my programming experience is in Java. I'm still learning about memory management. I'm ...

  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,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    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
    21,893
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    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, 11:27 AM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 07:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21