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

0

When I was beginning to learn pointers and dynamic allocation of memory 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;
}

And if we have allocated this memory in another function, where would an error occur if a call to exit() was required? Is it advisable to deallocate before exit() ?

    
asked by anonymous 30.09.2018 / 06:46

2 answers

1

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

This is not ideal for complex programs, which will run for a long time. When you use a Valgrind tool to find bugs, it lists all non-released allocations at the end of the program. It will be difficult to differentiate between what is "leak" and what is intentionally allocated allocation.

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

    
30.09.2018 / 07:00
3

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

Outside this extra flame she does not do anything 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 it is, everything will be released.

If you have open connections, it is the job of each service to realize that you have nothing more to communicate with and shut down. Of course, it's all controlled by the operating system in some way and it will shutdown somehow, even if not in the ideal way.

By default, understand that whatever you allocate must deallocate as early as possible, but never before it is possible, and every externally acquired resource is released (eg connections). A code that does not make these releases early is a wrong code by definition, even when it does not cause error. All malloc() ou algo semelhante deve ser pareado com um free () '.

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

So it is advisable to deallocate as soon as possible and rarely need to call exit() . She does not do what you think.

    
30.09.2018 / 13:22