nisse@lysator.liu.se (Niels Möller) writes:
To arrange for proper freeing before program exit in case (3) (or more complex real programs with that pattern), one would have to add an atexit call in main and potentially also in lots of other functions doing allocation. That would really clutter the code, for very little benefit.
One benefit is that you can run the program under valgrind and it won't report any memory leaks. I find this quite useful for self-tests, when you want to catch memory leaks in a library.
The case is less strong for examples though, however people tend to cut'n'paste example code and if the original is missing proper calls to free, so will the copied code, and it may end up in a library or further down the call path where the memory leak might actually hurt.
/Simon