3

Possible Duplicate:
When you exit a C application, is the malloc-ed memory automatically freed?

Pretty much the title. Do (modern) OSs automatically reclaim heap-allocated memory on program termination? If so, is freeing the memory a strict must or more of a just-follow-the-damn- standard-you-fool kinda thing?

What is the worst thing than can happen if I do not explicitly free the memory except that I end up wasting it while the application is in run?

Community
  • 1
  • 1
mmirzadeh
  • 6,893
  • 8
  • 36
  • 47

5 Answers5

2

if the app terminates the os will clean up the memory and resources. if you do not clean up memory with free( ) then heap-allocated memory will be used up as you request more and more. if it continues, the os will start using swap/page file to allocate. it u still continue os will grind down to a halt, or terminate the app with some kind of error

anon
  • 21
  • 1
2

Does OS reclaim memory on application exit in C? Yes.

If you don't free memory: your application may run out of memory & crash.

P.P
  • 117,907
  • 20
  • 175
  • 238
2

When an application terminates, all memory will be reclaimed by the OS. The reason people preach memory management techniques is because so long as the app is running, any memory it's asked for will remain in its posession (even if it's no longer being used). Unless, of course, you were nice enough to call free.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
2

Freeing memory before exiting is optional because the o/s will clean up for you.

Freeing unused memory while running is a good idea; it prevents the application from running out of memory (and having to exit). The difficulty is often working out which memory is unused.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

Yes. But if your program is a long running program, you not only crash your program eventually, but as the time progresses, you will also slowdown the system, assuming you use too much memory.

cgcoder
  • 426
  • 4
  • 11