Is freeing allocated memory needed when exiting a program in C

cfreemallocmemory-management

If I allocated memory in my C program using malloc and now I want to exit, do I have to free the allocated memory, or can I assume that since my entire program terminates, it will be freed by the OS?

I run in Linux environment.

Best Answer

Any modern operating system will clean up everything after a process terminates, but it's generally not a good practice to rely on this.

It depends on the program you are writing. If it's just a command line tool that runs and terminates quickly, you may not bother cleaning up. But be aware that it is this mindset that causes memory leaks in daemons and long-running programs.