Linked Lists

I just implemented a linked list of lists. My program runs correctly if I remove all the destructors. However, I get weird behaviour if I add destructors to correctly deallocate memory.

Is it necessary to have destructors. Will my program eventually crash if I don't add destructors?
Last edited on
If you don't have destructors when you need them, you will have memory leaks.

What's probably happening is you either are writing your destructor incorrectly, or you are failing to write appropriate copy ctor/assignment operator.
In my program I have a deleteNode command. So if I say deleteNode 31 in the command window, the node with value 31(if one exists) will be destroyed. In my code, I use delete inside the deleteNode function to be able to do this.

When I call delete from within this deleteNode function, does it call the destructor, and could this be the reason why my program crashes if I add destructors. Furthermore, if I have this deleteNode function, is it still necessary for me to add destructors?
When I call delete from within this deleteNode function, does it call the destructor


Yes. Delete calls the destructor of whatever you're deleting.

and could this be the reason why my program crashes if I add destructors


Again -- only if you're writing your destructor incorrectly (or failing to write appropriate copy ctors/assignment operators, but I don't think that's the problem here).

Furthermore, if I have this deleteNode function, is it still necessary for me to add destructors?


The whole point of destructors is that they are automatically called whenever the object is destroyed. This allows you to clean up/delete any memory the object owns.

If the object doesn't own any memory that needs deleting, then you probably don't need a destructor.
Topic archived. No new replies allowed.