0

I´ve been always told to free the heap memory allocated by malloc():

#include <stdlib.h>

#define width 5

int main(void)
{
    char* ptr = malloc(sizeof(*ptr) * width);

    /* some stuff with the heap object */

    free(ptr);

    return 0;
}

But now I´ve read in What REALLY happens when you don't free after malloc? that I don´t have to because the Operation System will automatically release the occupied memory after the program is terminated.

But why did my teacher told me to do so then? Is there any benefit of doing so?

  • 2
    It is _Very Good Practice_. Once you develop large programs that allocate lots of memory, you want to make sure the memory is released when no longer needed because otherwise your large program may run out of memory. – Paul Ogilvie Jan 26 '20 at 11:29
  • 1
    Also, being able to free all allocated memory without problems and terminating your program with a heap check that no memory was lost, means your program has no or less chance of memory bugs. – Paul Ogilvie Jan 26 '20 at 11:30
  • 1
    The question you ask—why your teacher told you to free memory even though general-purpose operating systems reclaim it—is answered at the link you cite. As it says, there is no absolute need to free memory before a process exits on an operating system that manages memory, but it is good practice for helping to avoid bugs (such as failing to free memory in loops, resulting in “leaks” that waste memory and may exhaust system resources) and for making code more easily modifiable and reusable (such as taking the bulk of the code in a program and making it into a subroutine in another program). – Eric Postpischil Jan 26 '20 at 11:48
  • So why should your question not be closed as a duplicate? Also, note that in practice there is often [little benefit to neglecting to free memory and letting the operating system do it](https://stackoverflow.com/a/55783367/298225) because releasing memory is often entangled with other aspects of winding-down a process, such as flushing file buffers. – Eric Postpischil Jan 26 '20 at 11:50

2 Answers2

5

But why did my teacher told me to do so then? Is there any benefit of doing so?

Think about what would happen if your program was a long-running process—like, say, a database server—and somewhere inside its guts there's a function that keeps allocating memory—like say, every time it processes a query—but never frees it. Or, more mundane, a desktop application like a word processor that you want to keep running while you work on something.

Yes, the OS will release all the resources used by a process after it terminates. But many programs are not of the short-lived-kind.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • This happened in a sudoku-brute-forcer I wrote. I'd malloc an int array of length 25 on every check (~100 bytes), and that ended up eating 15 GB of ram and a 2 GB swapfile, crashing the program before solving anything. – mindoverflow Feb 17 '20 at 16:04
1

In your example it doesn't matter, because as you say, the operating system will clean up for you when the program exits.

But it is highly recommended to do it regardless. Once your program gets even slightly more complicated than this example, you will encounter cases where you'll have to free your allocated memory to avoid the memory leaking.

Memory leaks happen when you've allocated memory but you no longer hold any pointers to it, and your program is still running. Since C has no garbage collection, neither the language nor the OS will clean up this memory until the program exits, because it has no way of knowing that you intend to clean it up, since you haven't called free(). This will be very problematic if you intend to write long-running programs.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Conor Taylor
  • 2,998
  • 7
  • 37
  • 69
  • 1
    There are no smart pointers in C. Referring to C++ will only confuse the OP. – wildplasser Jan 26 '20 at 11:50
  • 1
    I thought it would be helpful rather than confusing to mention C++ since it will hopefully help the OP understand the problem space he's confused with, which is not limited to C. – Conor Taylor Jan 26 '20 at 11:52