Thread: using delete in stacks

  1. #1
    Registered User
    Join Date
    May 2006

    Question using delete in stacks

    Let's say I have a class serving as a stack called Pole, with the class of stuff that it actually contains being Disc. That is, class Pole is a stack of class Disc. Furthermore let's say that the push function is void Pole-push(Disc* arg), and that the pop function is Disc* Pole-pop(). Furthermore I have a function called void Pole-empty(), which deletes all Discs in the Pole stack from memory and empties the Pole.

    I'm running into a little bit of an issue remembering how to completely empty the Pole of Discs with regards to whether I should or should not use the delete keyword.

    The specific problem I'm running into is illustrated in this example. Let's say I have a class that holds a Pole variable called aPole. Furthermore let's say two functions in that class are as follows:

    void func1()
    Disc aDisc;
    Disc anotherDisc;
    void func2()
    In this case, does the code in the definition for the void Pole-empty() function need to have the delete keyword to kill the Discs manually?

    I used to think of using delete if and only if the keyword new were used earlier. But here the keyword new is never used. But the Discs live on even after going out of scope (the exit of func1()). If aPole were to lose all track of those Discs from its empty() function being employed in func2() WITHOUT using the delete keyword, what's keeping those Discs from just wasting memory? Also wouldn't the destructor for Pole need to use the keyword delete?

    Thanks in advance!
    Last edited by jsrig88; 12-22-2009 at 09:47 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    I'll take a stab at this in the hope that the experts here will correct me wherever I'm wrong.

    First, like you said, it seems very strange to me to use delete if you've never used new. I'm not even sure what that would be telling the compiler to do since the object variables are supposed to have block scope.

    Anyhow, as I understand it, when you first declare your 2 Disc objects here, they are assigned a memory location and will be automatically collected as garbage when that block ends. If I wanted the memory to be freed up earlier, then I would create them using new and then destroy them explicitly.

    I'm not seeing why putting them on a Pole and taking them back off would make any difference in all that. Unless you allocate the memory for them dynamically one way or another, they're going to exist for the extent of the block where they're defined--and will also die with that block when it's over.

    I think in your code aDisc and anotherDisc would also have block scope if you popped them directly rather than running your empty() function.

    Anyhow, I'm not seeing why this would be an exception to the rule of using delete iff you used new.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    If you never call new, then the object lifetime is lexical, not dynamic, and a memory leak is impossible. I can say this without even seeing the implementation of Pole. No need to worry.

    You DO need to worry about valid lifetime, though. If the discs go out of scope before the Pole is destroyed, then the Pole now has pointers to destroyed objects, which is bad. There is nothing that you can do from within Pole to protect against this, aside from actually making copies of the discs when they are put into it.
    Last edited by brewbuck; 12-22-2009 at 10:36 PM.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #4
    Registered User
    Join Date
    May 2006
    Thanks for the replies. As you might imagine, I had walked around that problem when constructing my program, but I think it was quite important for me to gain that information, so as to prevent future issues.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM