Is necessary to do a free(string) before end a program?

This question already has an answer here:


ANSWERS:


To ensure your software doesn't have any memory leaks, yes, you should call free for any memory that you have allocated dynamically.

Memory will be reclaimed by the OS when the process terminates, and so it doesn't matter much unless you are running low on available memory. However, using free to release memory is very important if you're writing software that will run in the background, such as a daemon process.

It is recommended (almost universally) that the Right Way (tm) is to free your memory and not to rely on the OS to reclaim that space.


It is not necessary.

There are some good reasons to, as described in another answer. But there are also some reasons not to: the code to do the freeing takes extra time to write, debug, and maintain, and is an extra potential source of bugs.

It's not possible to give a universal answer that you should or shouldn't: it's a choice that each project has to make, depending on its own circumstances.



 MORE:


 ? Memory leak in malloc
 ? Is there a reason to call delete in C++ when a program is exiting anyway?
 ? Does calling free or delete ever release memory back to the "system"
 ? When a program terminates what happens to the memory allocated using malloc that is not free'ed?
 ? Segmentation fault when malloc/free appear in loop in C
 ? Some memory seems to be left allocated after malloc() and free()
 ? Intricacies of malloc and free
 ? Puzzling behavior, malloc and free(), with libuv
 ? Simple test of malloc and free with int pointer causes double free or corruption error
 ? Kernighan & Ritchie malloc free logic