Will the allocated memory be released after process exits?

L

lovecreatesbeauty

Hello experts,



When I allocate memory use malloc() and forget to call a corresponding
free(), does this allocated memory lose forever after program exits
(Can i regard it same as process exits? )?

Or will the allocated memory be released after program exits?



Sincerely,

lovecreatesbeauty
 
P

Peter Nilsson

lovecreatesbeauty said:
When I allocate memory use malloc() and forget to call a corresponding
free(), does this allocated memory lose forever after program exits
(Can i regard it same as process exits? )?

The standard does not specify this.*
Or will the allocated memory be released after program exits?

On modern desktop operating systems, yes. On more specialised
operating systems, perhaps not.

You should structure your code so that you can easily deallocate
any and all dynamic objects. However, many programs avoid actually
doing the deallocation on program exit because, one some systems,
it can needlessly cause the system to thrash.

However, the more complicated your program, the more sense it makes
to deallocate memory. If system resources are tied to given memory
allocations, then the deallocation routines in your code can (and
should) untie/close those system links as the same time.

So, the choice is yours.

* Certainly not in any way that a strictly conforming program could
ever tell.
 
P

Pacher R. Dragos

lovecreatesbeauty said:
Hello experts,



When I allocate memory use malloc() and forget to call a corresponding
free(), does this allocated memory lose forever after program exits
(Can i regard it same as process exits? )?

Or will the allocated memory be released after program exits?

If you include your code in a try/catch block it will be dealocated by
garbage collector. But this is implemented only in a few compilers as
a add-on, like lcc.
Please ermember that this has nothing to do with the C standard.
example
try
{
//code here
}

catch(/*Exception e*/)
{
//do something with the exception or throw it again
}

If you know java this will be very easy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top