freeing malloc memory in C

Hello,

I know that it is good to explicitly deallocate any dynamic memory in your program. But I was wondering what are the implications of not freeing any memory allocated via malloc which is used until program terminates.

Will the C runtime will automatically do the deallocation when program terminates and is still any problem (performance implications) with that?
Most operating systems will deallocate such memory when the program exits. The only downside of not deallocating the memory yourself is that your program (or some other program running at the same time) may run out of memory. Of course it can only happen if your program keeps allocating more and more memory and runs for the sufficient time.

PS. I assume that we are talking about C here. In C++ it is not good to deallocate the memory explicitly. Destructors should do it for you.
You should explicitly free memory to avoid memory leak. Do not rely on OS behavior. In C++ also, you have to explicitly free memory by delete if you have allocated memory from heap.

Gorav
http://www.kgsepg.com
what are the implications of not freeing any memory allocated via malloc which is used until program terminates
Ypur app can use all the memory on the system, starving itself and other applications of memory. Before it stops, it will slow down considerably as the OS starts paging to satisfy further demands for more memory.

Will the C runtime will automatically do the deallocation when program terminates
No, the OS cleans up beghind the process. If the OS is incapable of such cleanup, the OS will need to be restarted.
Topic archived. No new replies allowed.