Should I delete every node in the linked list?

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?
Assuming you used 'new' to create your nodes, you need to delete each one yourself.
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.
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.
Fair enough. The point is, make sure your code closes cleanly :)
Registered users can post here. Sign in or register to post.