Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > will the memory allocated by malloc get released when program exits?

Reply
Thread Tools

will the memory allocated by malloc get released when program exits?

 
 
keredil@gmail.com
Guest
Posts: n/a
 
      07-04-2005
Hi,

Will the memory allocated by malloc get released when program exits?

I guess it will since when the program exits, the OS will free all the
memory (global, stack, heap) used by this process.

Is it correct?

 
Reply With Quote
 
 
 
 
MJ
Guest
Posts: n/a
 
      07-04-2005
Hi
Yes it is correct when the program ends all the memory will get
released...
Mayur

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      07-04-2005
In article <(E-Mail Removed). com>,
MJ <(E-Mail Removed)> wrote:

>Yes it is correct when the program ends all the memory will get
>released...


It would be better to quote context.


Neither the C standard nor the POSIX standard explicitly define
exit() [or POSIX _exit()] as freeing memory. Nor does the C standard
define abort() as freeing memory.


In practice, I cannot think of any multi-process operating system
that does -not- free malloc()'d memory when the process ends.
I dunno -- MVS maybe?

I seem to recall (perhaps incorrectly) that it is not certain that
memory will be freed at process end when one is running in an
embedded environment -- if your toaster-control program somehow
exits [e.g., toaster on fire?] then what meta-process is there to
free the memory?


The original poster asked about malloc()'d memory, not about
"all the memory". The phrase you used, "all the memory", could
be construed to include shared memory segments, mmap()'d segments
and other non-malloc()'d forms -- forms that lie outside the C standard
but which exist on numerous systems. Shared memory segments in particular
are not necessarily released when the creating process terminates.
--
Look out, there are llamas!
 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      07-04-2005

"MJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi
> Yes it is correct when the program ends all the memory will get
> released...
> Mayur
>


That's not necessarily true. Some versions of Windows do, others do not.
Unix does (At least the ones that I have used do). VxWorks does not.

REH


 
Reply With Quote
 
Sensei
Guest
Posts: n/a
 
      07-04-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Will the memory allocated by malloc get released when program exits?
>
> I guess it will since when the program exits, the OS will free all the
> memory (global, stack, heap) used by this process.
>
> Is it correct?


It's platform-dependent. Not every OS does it.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-04-2005
<(E-Mail Removed)> wrote
>
> Will the memory allocated by malloc get released when program exits?
>
> I guess it will since when the program exits, the OS will free all the
> memory (global, stack, heap) used by this process.
>
> Is it correct?
>

A decent operating system will do this. If the OS doesn't, the user deserves
to have to reboot, for buying such a trashy system.

You should free all memory in normal operation, to make it easier to detect
leaks. However if the program encounters an error, don't hesitate to just
exit(EXIT_FAILURE) if program logic makes this the natural thing to do.


 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      07-04-2005
In article <(E-Mail Removed) .com>,
(E-Mail Removed) wrote:

> Hi,
>
> Will the memory allocated by malloc get released when program exits?
>
> I guess it will since when the program exits, the OS will free all the
> memory (global, stack, heap) used by this process.


Someone will probably give you some answer, but the other question is:
Should you rely on malloc'ed memory being released when the program
exits?

First of all, you can't just keep on allocating memory. Your program
will either run out of memory at some point, or it will get slower and
slower if you allocate more and more memory and don't call free () when
it is not used anymore.

But you should also consider that what is one program today, might be a
tiny component of a program next year. So if you relied an the operating
system releasing memory when the program exits, you now have a function
that doesn't release its memory. And if you call it repeatedly, you run
into trouble.

It is best to always match allocation and deallocation of memory.
 
Reply With Quote
 
Mark F. Haigh
Guest
Posts: n/a
 
      07-04-2005
Malcolm wrote:
> <(E-Mail Removed)> wrote
> >
> > Will the memory allocated by malloc get released when program exits?
> >
> > I guess it will since when the program exits, the OS will free all the
> > memory (global, stack, heap) used by this process.
> >
> > Is it correct?
> >

> A decent operating system will do this. If the OS doesn't, the user deserves
> to have to reboot, for buying such a trashy system.


Many RTOSs do not free memory on program exit. For these systems, it's
a design tradeoff, not a quality issue.

>
> You should free all memory in normal operation, to make it easier to detect
> leaks. However if the program encounters an error, don't hesitate to just
> exit(EXIT_FAILURE) if program logic makes this the natural thing to do.


Please don't. Always free all memory you allocate.


Mark F. Haigh
(E-Mail Removed)

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      07-04-2005
In article <(E-Mail Removed). com>,
Mark F. Haigh <(E-Mail Removed)> wrote:
>Malcolm wrote:


>> You should free all memory in normal operation, to make it easier to detect
>> leaks. However if the program encounters an error, don't hesitate to just
>> exit(EXIT_FAILURE) if program logic makes this the natural thing to do.


>Please don't. Always free all memory you allocate.


Ah? Even if you caught a SIGSEGV?
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-04-2005

"Mark F. Haigh" <(E-Mail Removed)> wrote
>> However if the program encounters an error, don't hesitate to just
>> exit(EXIT_FAILURE) if program logic makes this the natural thing to do.

>
> Please don't. Always free all memory you allocate.
>

Unfortunately this advice, though well-meaning, is impractical. Errors occur
mid processing, often leaving structures in a corrupted or half-build state.
So freeing everything can be complicated. Then it is even more difficult to
test the freeing code, and it might itself generate errors.

So unless freeing all memory in all circumstances is an absolute
requirement, which is unusual, it is much better to simply exit with an
error message if it becomes necessary to abort.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to check whether malloc has allocated memory properly in caseif malloc(0) can return valid pointer Gene C Programming 0 12-20-2010 05:33 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
Overwriting Allocated Memory from malloc() tweak C Programming 19 07-10-2004 12:33 PM
some queries on freeing memory allocated using malloc Hassan Iqbal C Programming 3 09-25-2003 02:53 PM



Advertisments