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:
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!
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.
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.
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.