Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need of deallocating memory in C?

I may be misinformed, but to my knowledge the OS cleans up memory after a program quits or crashes.

If so, how useful is it to deallocate memory at the end of a program? I understand that if a program is running and deallocating is neglected that memory could become "full", but if a program is already going to end and the OS deallocates all memory used by the program, what is the point of deallocating that memory manually?

like image 588
Rik Schaaf Avatar asked Jun 15 '14 10:06

Rik Schaaf


People also ask

Why do we need to deallocate memory?

If you use a tool to detect memory leaks or similar problems, then deallocating memory will clean up the output of such tools. In some less complex operating systems, the operating system may not reclaim memory automatically, and it may be the program's responsibility to reclaim memory before terminating.

Why do we need memory allocation in C?

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.

What do you use deallocate memory?

The "free" method in C is used to deallocate memory dynamically. The memory allocated by malloc() and calloc() is not automatically deallocated. As a result, the free() function is utilized anytime dynamic memory allocation occurs. It frees up memory, which helps to prevent memory waste.

What happens if you don't free memory in C?

If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).


2 Answers

Quoting from Memory Deallocation Issues in C:

The operating system is responsible for maintaining the resources of an application, including its memory. When an application terminates, it is the operating system’s responsibility to reallocate this memory for other applications. The state of the terminated application’s memory, corrupted or uncorrupted, is not an issue. In fact, one of the reasons an application may terminate is because its memory is corrupted. With an abnormal program termination, cleanup may not be possible.

With this said, there may be other reasons why memory should be freed when a program terminates normally:

  • The conscientious programmer may want to free memory as a quality issue. It is always a good habit to free memory after it is no longer needed, even if the application is terminating.
  • If you use a tool to detect memory leaks or similar problems, then deallocating memory will clean up the output of such tools.
  • In some less complex operating systems, the operating system may not reclaim memory automatically, and it may be the program’s responsibility to reclaim memory before terminating.
  • Also, a later version of the application could add code toward the end of the program. If the previous memory has not been freed, problems could arise.
like image 169
haccks Avatar answered Nov 02 '22 09:11

haccks


Cleanliness.

You could, of course, not bother going through your cleanup and let the system handle it. However, if you do this it is essentially impossible for you to trace memory leaks in your program since you can't run it and see whether anything is left allocated at the end. If, on the other hand, you ensure a clean shutdown you can know whether there are any leaks by running it and seeing whether anything is left allocated at the end. Since for any non-trivial program likely to be running for some time memory leaks are something to avoid, doing it in this clean fashion leads to benefits.

Additionally, it's also just part of ensuring your program shuts down cleanly with any persistent state left in the right condition and any external resources freed (although most modern OSs will clean that up these days) because you're going through an orderly shutdown rather than just cutting and running.

like image 4
Jack Aidley Avatar answered Nov 02 '22 09:11

Jack Aidley