By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Will the allocated memory be released after process exits?

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
lovecreatesbeauty wrote:

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.

--
Peter

Nov 15 '05 #2

P: n/a

lovecreatesbeauty wrote:
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.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.