Should I delete large collections of trees in C ++ at the end of the program, or should I leave it to the OS?

This might be a little silly question - should I call delete

the huge map / install at the end of the program?
Assuming map / set is needed throughout the entire program ( delete

- last line before returning) and the size is really huge (> 4GB). The call delete

takes a long time and does not matter from my point of view (the memory cannot be released earlier), am I wrong? If so, why?

+3


source to share


3 answers


There is no guarantee in the C and C ++ standards of what happens after your program exits. Including there is no guarantee that something is cleared. For example, some small real-time operating systems will not automatically clean up. Therefore, in theory at least, your program should definitely do delete

everything you do new

to fulfill this commitment as a complete and portable program that can run forever.

It is also possible that someone takes your code and puts a loop around it so that your tree is now created a million times and then goes looking using a "reliable persuader" aka baht baht when they find out WHY they now have it runs out of memory after 500 iterations.



Of course, like all things, this can be argued in many different ways, and it really depends on what you are trying to achieve, why you are writing the program, etc. My compiler project is losing memory like a sieve because I am using exactly the memory management method you describe (partly because keeping track of the life of each dynamically allocated object is quite difficult, and partly because "I can't be bothered." that if someone really wants a good pascal compiler they won't go to my code anymore).

Actually, my compiler project creates a lot of different data structures, some of which are trees, arrays, etc., but mostly none of them do any cleanup afterwords. This is not as easy to fix as the case of building a large tree, which requires every node deletion. Conceptually, however, it all boils down to "cleanup" or "no cleanup", which in turn boils down to "ok who will use / modify the code and what you know about the environment it will run in".

+7


source


As long as your program remains roughly the same, since memory is used for all execution, it doesn't really matter.

However, if someone tries to take your program and turn it into a component of some other program without freeing up memory, it can lead to a huge memory leak.



So, to be safe as well as to be more organized, always release what you give out. This is very easy in your case, so there are no downsides - just upside potential.

+1


source


If you are using C ++ STL then there is no need for explicit removal, because map / set will automatically manage a bunch of memory for you. You are not actually allowed to delete these cards / set as they are not pointers.

For large objects stored inside a map / set, you can use smart pointers when constructing those objects, and then you no longer need to call their destructors. Memory leak may not be a problem for a toy program, but it is not acceptable for real programs as they can run for a long time or even forever.

+1


source







All Articles