[try Beta version]
Not logged in

 
Should I delete every node in the linked list?

Jul 10, 2014 at 7:23pm
Hi again,

Still toying with my self-coded linked list class and now another question: should I delete each node of my class with the delete in the class destructor or is it done automatically when the main() function ends?
Jul 10, 2014 at 7:45pm
Assuming you used 'new' to create your nodes, you need to delete each one yourself.
Jul 10, 2014 at 7:55pm
Any modern OS will automatically free up any memory your application was using when it exits. This does not mean you should get in the habit of not freeing your own memory, though. Especially on a data structure that is meant to be used in any number of applications. I recommend looking into smart pointers.
Jul 10, 2014 at 8:56pm
Any modern OS will automatically free up any memory your application was using when it exits.
This is true. However, the system will only free system resources, and won't call any destructors. This is a problem when the destructors do non-trivial things, like write data to the file system, or send messages to other processes. Not calling a destructor in such cases may easily leave user data in an unusable state, or hang other processes.
Jul 10, 2014 at 9:33pm
Fair enough. The point is, make sure your code closes cleanly :)
Topic archived. No new replies allowed.