Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > program termination/memory leaks

Reply
Thread Tools

program termination/memory leaks

 
 
J. Campbell
Guest
Posts: n/a
 
      09-11-2003
what happens to allocated memory when a program terminates before the
memory is released. For example:

int main(){
int* array;
int a_size = 1000;
array = new int[a_size];
for(int i = 0; i < a_size; ++i){
if(i > (a_size/2))
return 0; //oops...ended before delete...is this a problem?
}
delete[] array;
return 0;
}

If this is a leak, does the same thing happen when you "ctrl-break" a
running program?

And the same question when an array is placed on the heap by a class
constructor, with delete being called in the destructor. What happens
when the program stops before the object goes out of scope?
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-11-2003
J. Campbell wrote:
> what happens to allocated memory when a program terminates before the
> memory is released. For example:
>
> int main(){
> int* array;
> int a_size = 1000;
> array = new int[a_size];
> for(int i = 0; i < a_size; ++i){
> if(i > (a_size/2))
> return 0; //oops...ended before delete...is this a problem?
> }
> delete[] array;
> return 0;
> }
>
> If this is a leak, does the same thing happen when you "ctrl-break" a
> running program?


The operating system reclaims all resources used by the process when the
process exits.

>
> And the same question when an array is placed on the heap by a class
> constructor, with delete being called in the destructor.


You must explicity delete all object you create with new. Only
automatic, static or global variables are deleted automatically by the
compiler.


What happens
> when the program stops before the object goes out of scope?


"program stops" ?

There are various ways a program may stop. Which ones are you referring
to ?

a) Power failure

b) low level exit

c) stopped in the debugger

e) terminated asynchronously

g) suspended

.... probably more


 
Reply With Quote
 
 
 
 
dwrayment
Guest
Posts: n/a
 
      09-11-2003
to answer your question. YES that is a memory leak. dont do that.




"J. Campbell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> what happens to allocated memory when a program terminates before the
> memory is released. For example:
>
> int main(){
> int* array;
> int a_size = 1000;
> array = new int[a_size];
> for(int i = 0; i < a_size; ++i){
> if(i > (a_size/2))
> return 0; //oops...ended before delete...is this a problem?
> }
> delete[] array;
> return 0;
> }
>
> If this is a leak, does the same thing happen when you "ctrl-break" a
> running program?
>
> And the same question when an array is placed on the heap by a class
> constructor, with delete being called in the destructor. What happens
> when the program stops before the object goes out of scope?



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-11-2003
Gianni Mariani wrote:

> The operating system reclaims all resources used by the process when the
> process exits.
>


Oh? As far as I know, this is not required by the standard. I've seen a
system that lost memory that was not explicitly freed. It was pretty
much gone forever, even if the system was turned off and back on. The
only way to reclaim it was to wipe the entire memory (the equivalent of
formating your hard disk).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      09-11-2003
Kevin Goodsell wrote:
> Gianni Mariani wrote:
>
>> The operating system reclaims all resources used by the process when
>> the process exits.
>>

>
> Oh? As far as I know, this is not required by the standard. I've seen a
> system that lost memory that was not explicitly freed. It was pretty
> much gone forever, even if the system was turned off and back on. The
> only way to reclaim it was to wipe the entire memory (the equivalent of
> formating your hard disk).


That'l teach you for using Windows ! :^)

Right you are - it may be that you are writing the "operating system" !

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      09-11-2003
J. Campbell wrote:

> What happens to allocated memory
> when a program terminates before the memory is released?


The operating system reclaims all memory allocated for the process
whether the program terminates normally or not.

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-11-2003
Gianni Mariani wrote:
> Kevin Goodsell wrote:
>
>> Gianni Mariani wrote:
>>
>>> The operating system reclaims all resources used by the process when
>>> the process exits.
>>>

>>
>> Oh? As far as I know, this is not required by the standard. I've seen
>> a system that lost memory that was not explicitly freed. It was pretty
>> much gone forever, even if the system was turned off and back on. The
>> only way to reclaim it was to wipe the entire memory (the equivalent
>> of formating your hard disk).

>
>
> That'l teach you for using Windows ! :^)
>
> Right you are - it may be that you are writing the "operating system" !
>


The system in question was actually a calculator. It had its own OS, but
it wasn't really designed for third-party programming. Since its state
is maintained when the power is off (either by the main batteries or a
separate backup battery), it couldn't really be 'rebooted' without
deleting everything in memory - which is where everything short of the
OS itself is stored. (Well, that's not entirely true - there was some
non-volatile RAM that wasn't used by the OS that could be used for
archiving 'files' and such.)

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Patrick Hoonhout
Guest
Posts: n/a
 
      09-12-2003

> The system in question was actually a calculator. It had its own OS, but
> it wasn't really designed for third-party programming. Since its state
> is maintained when the power is off (either by the main batteries or a
> separate backup battery), it couldn't really be 'rebooted' without
> deleting everything in memory - which is where everything short of the
> OS itself is stored. (Well, that's not entirely true - there was some
> non-volatile RAM that wasn't used by the OS that could be used for
> archiving 'files' and such.)
>


This is the std C++ group. OS calculator programming questions should go to
the appropriate calculator news group... :*)


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-12-2003
On Thu, 11 Sep 2003 13:32:41 -0700, "E. Robert Tisdale"
<(E-Mail Removed)> wrote in comp.lang.c++:

> J. Campbell wrote:
>
> > What happens to allocated memory
> > when a program terminates before the memory is released?

>
> The operating system reclaims all memory allocated for the process
> whether the program terminates normally or not.


Can you cite the reference to the clause of the C++ standard that
states that requirement?

Alternatively, can you provide proof that you have tested this on
every single operating system in existence and found it to be true?
And proof that an operating system due to be released next year it
will also be true?

Neither C++ no any other language standard can impose requirements on
the operating system that executes programs.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
J. Campbell
Guest
Posts: n/a
 
      09-12-2003
Thanks for the advice, guys.

Gianni Mariani wrote:
"The operating system reclaims all resources used by the process when
the
process exits."

dwrayment wrote:
"YES that is a memory leak."

E Robert Tisdale wrote:
"The operating system reclaims all memory allocated for the process
whether the program terminates normally or not."


Hmm...do I believe the majority? My real concern was that I was doing
some tests where I had created arrays containing 10's of MBs, but
"ctrl-break"ed the program, as I was just testing and didn't want to
wait for it to finish running. I just wanted to make sure that I
wasn't losing resources by doing this. Sounds like it's no prob.

So...this is a related OT Q. Sorry Kevin G for the OT post, as
you seem to be (gracefully, and helpfully) wearing the comp.lang.c++
Protocol Police badge & baton at the moment. Anyway...if the OS
reclaims resources upon process termination, why does my PC need a
periodic reboot??

Thanks again.
 
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
find memory leaks in running program Marco Hornung Python 1 12-07-2010 09:45 PM
leaks unloading AppDomain greg ASP .Net 1 06-13-2005 12:45 PM
Website to Test for Security Leaks Herb Firefox 3 04-18-2005 01:48 PM
TextReader in Cache Leaks? Chan ASP .Net 3 11-10-2003 01:43 PM
memory leaks with firebird? astromannix Firefox 2 07-26-2003 12:10 AM



Advertisments