[ACCEPTED]-Is freeing allocated memory needed when exiting a program in C-free

Accepted answer
Score: 21

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

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

Score: 12

It can be a good design and very efficient 13 to simply exit and allow the operating system 12 to clean everything up. Apple OS X now does this by default: applications are 11 killed without notice unless the application 10 sets a "don't kill me" flag.

Often, freeing 9 every memory allocation takes significant 8 time. Some memory pages may have been swapped 7 out and must be read back in so they can 6 be marked as free. The memory allocator 5 has to do a lot of work updating free memory 4 tracking data. All of this effort is a waste 3 because the program is exiting.

But this 2 must be done by design and not because the 1 programmer has lost track of allocated memory!

Score: 3

In any case it will be freed by the operating 7 system upon process termination. So you 6 don't need it, but since it is a good practice, why 5 don't you do it anyway? :)

Actually with 4 complex code I wouldn't risk to don't release 3 something which I'm not sure at 100% that 2 will be useless because program exits afterwards. So 1 for any minimal doubt just free it.

Score: 2

The operating system will reclaim the memory 4 so you don't need to free it.

Most programs 3 do free memory though because if you don't 2 free any memory then you are liable to have 1 problems caused by these intentional leaks.

Score: 2

Yes you can assume that.

Although it is a 3 good practice to deallocate the memory immediately 2 after it is not needed, even for software 1 that runs for a short time only.

Score: 1

Linux will free the allocated memory and 1 close the file descriptors on process termination.

Score: 0

The OS will reclaim the memory, however 6 it's good practice to free things if you 5 expect they'll run out of scope before you 4 malloc something else. However, you can 3 more or less rely upon the termination of 2 the program to deal with memory management 1 for you.

Score: 0

Always free your allocated memory since 4 that the operating system will hold less 3 memory for no reason. It is very noticed 2 in small operating systems that holds small 1 memory size.

More Related questions