Questions regarding heap deallocate

S

Sean

Here is a confusion about free() and delete. Someone told me that the memory
chunk allocated in c++ will be automatically released back to system for
future use. I haven't been convinced.

In C,
main()
{
int* p
p = malloc(100*sizeof(int));
return 0; //A
}
(1) After my program returns, will this chuck of heap be released back to
system?
(2) if I change line A to exit(1), will this chuck of heap be released back
to system?

In C++
int main()
{
int* p = new int[100];
return 0; //B
}

(3) someone told me that this chuck of heap has been released back to system
after the program exits, EVEN THOUGH there is no delete operation. Is it
TRUE or not?
(4) If it's true: if I change line B to exit(1), will the memory be
released?

Thank you
 
G

Gianni Mariani

This question is not addressed by the C++ standard and hence is off
topic in this NG. Perhaps comp.progrmming would be a better forum.

I've taken a swag at it for you - see below.
Here is a confusion about free() and delete. Someone told me that the memory
chunk allocated in c++ will be automatically released back to system for
future use. I haven't been convinced.

In C,
main()
{
int* p
p = malloc(100*sizeof(int));
return 0; //A
}
(1) After my program returns, will this chuck of heap be released back to
system?

This depends on your operating system. If you are writing the kernel,
you can probably bet it will not be released, if you are writing an
application on Unix/BSD/Linux/AIX/Solaris/HPUX/VMS/VM etc etc is will be
released and on Win32 it's released most of the time.
(2) if I change line A to exit(1), will this chuck of heap be released back
to system?

On Unix/Win32 Reclaiming memory (and other resouces) happens whenever
the process exits, no matter how it exits. (call to exit or kill signal).
In C++
int main()
{
int* p = new int[100];
return 0; //B
}

(3) someone told me that this chuck of heap has been released back to system
after the program exits, EVEN THOUGH there is no delete operation. Is it
TRUE or not?


Same exact issue as malloc/free comments to (1) apply here.
(4) If it's true: if I change line B to exit(1), will the memory be
released?

Same exact answer to (2) applies here.
 
J

Jack Klein

Here is a confusion about free() and delete. Someone told me that the memory
chunk allocated in c++ will be automatically released back to system for
future use. I haven't been convinced.

I'm glad you told us. We've all been waiting impatiently here to find
out whether you have been convinced or not. Thank you for ending the
unbearable suspense.

And who was it that told you this? Your mail delivery person? Car
mechanic? Hair stylist? What reason do you have to give any credence
to that person's pronouncements?
In C,
main()

Under the current C standard the above definition of main() is illegal
and requires a diagnostic. There is no such thing as implicit int
anymore, every declarator is required to specify a type:

int main()
{
int* p

p = malloc(100*sizeof(int));
return 0; //A
}
(1) After my program returns, will this chuck of heap be released back to
system?

Who knows? The C standard does not and cannot specify what the
conditions are before a C program is invoked, or after it ends. In
fact, the C standard does not and cannot specify what any operating
system does.
(2) if I change line A to exit(1), will this chuck of heap be released back
to system?

Same answer.
In C++
int main()
{
int* p = new int[100];
return 0; //B
}

(3) someone told me that this chuck of heap has been released back to system
after the program exits, EVEN THOUGH there is no delete operation. Is it
TRUE or not?

The C++ standard has no more control over the behavior of any
operating system than the C standard does.
(4) If it's true: if I change line B to exit(1), will the memory be
released?

I have no idea. Consult your compiler and operating system
combination.
Thank you

Many operating systems automatically reclaim all memory allocated to a
program when the program ends. Others do not. Neither the C nor C++
standard is an any position to try and force operating systems to
operate in any particular manner.

--
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top