Freeing Allocated Memory

One criticism I receive is that my code examples, from both my online training material as well as in my C programming books, fail to free any allocated memory before the program quits. This assertion is correct, and I have a darn good reason why!

First, the pedagogy: When a program runs, the operating system allocates it a chunk of memory and some processor time. These resources become the sandbox in which the program plays. It cannot venture outside of its box, lest some nasty error occur, the program is cruelly terminated, and the operating system becomes cross enough to splash a message on the screen. Otherwise, the program goes on its merry way, does it thing, and then it quits. When the program quits, the operating system repossess the allocated resources like an evil bill collector in a Hallmark holiday movie.

Within its realm, however, the program is free to use its own memory. In a C program, memory is allocated via the malloc() function or one of its siblings. In school, they instruct you that all memory allocated in the program must be freed before the program quits. This mantra is true: Use the free() function to loosen the program’s grip on the allocated memory. This function doesn’t zap out the memory’s contents. Instead, it makes the memory chunk available for other uses, keeping it from lazily lingering like a drunk guest at a house party who refuses to leave, even when you turn off the lights while wearing your pajamas.

In my code, I don’t bother freeing memory allocated in the main() function, especially when it’s used only once and used consistently throughout the program. My reason is that the memory is cleared when the program quits. But the argument I hear against my approach is that it’s bad coding practice. Perhaps it is.

In this sample code, I commit the sin of not freeing a 1K memory chunk.

2022_04_16-Lesson-a.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char *buffer;

    buffer = malloc(1024);
    if( buffer==NULL )
    {
        fprintf(stderr,"Unable to allocate memory\n");
        exit(1);
    }
    puts("Memory allocated");

    return(0);
}

The malloc() function at Line 8 allocates 1K of memory. The if test confirms that the memory was allocated. That’s it. Nothing is done with the memory chunk.

The program runs, of course. It sets aside 1K of memory. The program quits. The memory is de-allocated.

But.

The following code works the same, though it frees the allocated memory before the program quits, before the operating system frees it anyway:

2022_04_16-Lesson-b.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char *buffer;

    buffer = malloc(1024);
    if( buffer==NULL )
    {
        fprintf(stderr,"Unable to allocate memory\n");
        exit(1);
    }
    puts("Memory allocated");

    free(buffer);
    puts("Memory freed");
    return(0);
}

As I wrote earlier, I find the addition of the free() statement in this sample code to be redundant. Sure, it works and it causes nothing bad to happen. The real issue occurs when you allocate memory in a function and forget to free it. This is the problem your programming professors and instructors are trying to get you to avoid by always freeing every allocated byte in a program.

In next week’s Lesson I explore this concept further, demonstrating when it’s absolutely necessary to free memory temporarily allocated in a function.

2 thoughts on “Freeing Allocated Memory

  1. Could you please write a post about the advantages and disadvantages variable-length array (VLA) in C? Thanks

Leave a Reply