What is the purpose of the free () function?

  • In which cases should it be used?
  • Is there an alternative?
  • Is it recommended to use?
asked by anonymous 12.12.2014 / 12:18

2 answers


What is

This is the function to deallocate allocated memory in the heap (there it explains why heap is required). It is often used in pairs (not necessarily directly) with malloc() .


In addition to the trivial codes you can not avoid using malloc() (and your sisters calloc() and realloc() , so you can not avoid using free() .

Where you use malloc() you have to use free() . Of course in things simple things may not use. It is wrong but will not cause problems. Even in simple things it is the good programmer's habit to always do it right.

So you should always use it. There is no escape from it unless you want to compromise all computer memory. Imagine that you are allocating memory and always leaving it there. If the application runs for some time and does something useful in a very short time many gigabytes of memory will be little for your application. A program generates a lot of junk (data that was needed at one point but no more).

When to use

One of the recommendations is to use both functions together within the same function to avoid getting lost and forgetting to deallocate, or worse, deallocate what still can not and will still be used by the application.

Of course, this is not always possible. Then care needs to be redoubled. Documentation, caution and much testing will be needed.

In C ++ this gets even more complicated because even though it is in the same function, in the same scope an exception can be thrown and bypass the program flow without you knowing, and a call to the free() function or use of the delete may not run, as expected. Hence, a little more automatic management becomes fundamental.


The alternative is to use automatic management. In the background this management will use free() in some way since this function is used to communicate with the operating system API to free memory.

Garbage collector

In some cases this automatic management can be a garbage collector but it is not usually used in C / C ++ since the language does not help. In addition, he often changes the characteristics of language use against their culture.


In C ++ there is even an alternative. It is actually recommended to use delete (which internally ends up using free() but in a more organized way) and not free() .

In fact in C ++ the free() should be avoided in favor of the delete or even delete[] operator that deallocates arrays . The original C function is very crude for C ++ standards. It can still be used, since it needs C compatibility, needs to communicate with the OS, and in some cases greater flexibility may be needed (quite rare).

Smart pointers

Furthermore, it is recommended to leave the semiautomatic management using own classes to control memory management. These classes make the allocation and know when they need to deallocate leaving the programmer free of this decision. These classes are called smart pointers . They help a lot but do not do miracles. If you forget to use them or use the wrong class for this you may get unwanted results.

Another language

If you do not want to take care of memory management, the alternative is to use a language with managed memory, as is more common nowadays. C / C ++ should be used when you need the power, flexibility and speed of these languages. Often the choice of one is just to be able to manage the memory as you want.

12.12.2014 / 12:43

The function void free(void *ptr) serves to free memory previously allocated by calloc , malloc , or realloc .

It should be used in all cases where it is no longer necessary to use a previously allocated memory space.

I'd say it's mandatory to use. Not in the sense of not working, but in the sense of maintaining the integrity of your memory.

In C language access to memory is of extreme importance, if we do not have this concern will screw up for sure.

Note: Always keep memory allocation in mind so that it does not allocate less memory than is necessary for what you want. And always take extra care to free up any memory space you no longer need.

12.12.2014 / 12:42