Is it always good to de-locate the memory before a "sudden" output of the program with the Exit function call?

Asked

Viewed 208 times

0

When I was beginning to learn pointers and dynamic memory allocation in C, I was told that all memory allocated in the program is out of focus when it is finished. 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 in case an error occurs a call is required to exit()? It is recommended to make a relocation before exit()?

2 answers

3

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

Other than this extra flame, she doesn’t do 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 everything will be released.

If you have open connections it is the function of each service to realize that there is nothing else communicating with it and closing. Of course, it’s all controlled by the operating system somehow and it will shut down somehow, even if not in the optimal way.

By default understand that everything you allocate, should displace as soon as possible, but never before it is possible, and every resource acquired externally 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 error. All malloc() or something similar should be paired with a free().

So C programmers know that, in general, they should not allocate memory in other functions, the allocation takes place the first place they need and so the person knows they need to give a free() there, including 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 total commitment to it. Go to another language if you want ease to manage memory. Because it complicates a lot when you can’t keep this simple control of allocating/dislocating within the function, and it has several cases like this, where the allocation depends on the lifetime of an object and not the function. So one of the best computer inventions was the garbage collector.

So it is recommended to relocate as soon as possible and rarely need to call exit(). She doesn’t do what you think.

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 short running small program 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 un-released allocations at the end of the program. Then it will be difficult to differentiate what is "Leak" and what is allocation left on purpose.

As every great program was one day a small and unpretentious program, it’s good to start by doing the right thing from the beginning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.