Is it always good to deallocate memory before a "abrupt" exit of the program with the call of the exit function?

When I was starting to learn pointers and dynamic memory allocation in C, I was told that all memory allocated in the program is deallocated when it is terminated. Ex:

#include <stdlib.h>

int main(void){

    int *vet=calloc(10, sizeof(int));

    //...faz alguma coisa com esse vetor

    //assim que o programa é finalizado toda
    //toda a sua memória é desalocada automaticamente...

    return 0;
}

What if we have allocated this memory in another function, where in the event of an error would a call to exit() be required? Is it recommended to do a deallocation before exit()?

Author: Maniero, 2018-09-30

2 answers

The memory allocated by the program belongs to the process; when the process dies, all resources used by it are released. So, a small program of short execution can "relax" in memory management without consequences.

This is not ideal for complex programs, which will run for a long time. When using a Valgrind-like tool to find bugs, it lists all non-released allocations at the end of the program. Then it will be difficult to differentiate what it is "leak" and what is allocation left on purpose.

Since every big program was once a small and unpretentious program, it's good to start by doing the right thing from the beginning.

 1
Author: epx, 2018-09-30 05:00:06

See function documentation exit(). In fact it does some cleaning when it is called, even it can register some things to be executed when it is called. If you need something to run, you should call her. But remember that the application can break before and it will not be called even if you want.

Outside this extra flame she does nothing too much. Your application does not own the memory allocated by it, and the operating system will release everything at the end of the process, so no matter where you allocate everything will be released.

If you have open connections it is the function of each service to realize that it has nothing else communicating with it and terminate. Of course, it is all controlled by the operating system in some way and it will do the shutdown in some way, even if not in the optimal way.

By default understand that everything you allocate, you should deallocate as soon as possible, but never before that is possible, and each externally acquired resource is released (connections for example). A code that does not make these releases early is wrong code by definition, even when it does not cause an error. All malloc() ou algo semelhante deve ser pareado com umfree()`.

So C programmers know that, in general, should not allocate memory in other functions, the allocation occurs the first place it needs and so the person knows that he needs to give a free() there, including with control to have an output of this function for only one place. Managing memory in C is extremely difficult and so only use this language if you have total commitment to it. Go to another language if you want ease to manage memory. Because it complicates a lot when you can not keep this simple control of allocate / deallocate within the function, and has several cases like this, where the allocation depends on the life time of an object and not the function. So one of the best inventions of computing was the garbage collector.

Then it is recommended to vacate as soon as possible and rarely need to call exit(). She doesn't do what you think.

 3
Author: Maniero, 2018-09-30 11:22:25