473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Questions regarding heap deallocate

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
Jul 19 '05 #1
2 3065

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.

Sean wrote:
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.
Jul 19 '05 #2
On Sun, 21 Sep 2003 17:33:20 -0500, "Sean" <ya****@purdue.edu> wrote
in comp.lang.c++:
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
This is undefined behavior if <stdlib.h> is not included, or a proper
prototype for malloc() is not in scope.
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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
0
by: roberto.bacenetti | last post by:
My program 's virtual memory keeps growing yet I could not find any place where I did not deallocate memory I isolated a piece of code and single stepped through the CRT DEBUG HEAP handler: this is...
13
by: Vijay | last post by:
Hi All, I am learning C++ and have one question. Using free or delete we can release the memory. After releasing memory where this released memory will go.. Does it go to back operating...
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
1
by: =?Utf-8?B?Tm90cmUgUG91YmVsbGU=?= | last post by:
I apologize if this question has been asked before (I imagine it has). But, I'd like a definitive response. Is it possible using C++/CLI to allocate a (normally) managed reference type on the...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.