473,788 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should I delete these memory before exit process

I just find i can't understand the code like this
int main()
{
int *p=new int;
//do something
delete p;
return 1;
}
I use new operator allocate some memory in the heap,now i don't need
it,but our process also need to be killed.The heap is in our process'
address space,so i think the os should release our heap when it kill
process,isn't it?why i should delete it by myself?
Is there have some problems if i don't delete 'p'?

Thanks for your help.

Oct 10 '06 #1
9 3923
Osamede Zhang wrote:
The heap is in our process'
address space,so i think the os should release our heap when it kill
process,isn't it?why i should delete it by myself?
Is there have some problems if i don't delete 'p'?
Sure. You will constantly lose memory while your program's running.

Oct 10 '06 #2
Yes, you should destroy the pointer.

the created object by new can be release, but this is a good habit
for a c plus plus programer. Your program is easy, if it is very
difficult this is don't imaginative.

Osamede Zhang wrote:
I just find i can't understand the code like this
int main()
{
int *p=new int;
//do something
delete p;
return 1;
}
I use new operator allocate some memory in the heap,now i don't need
it,but our process also need to be killed.The heap is in our process'
address space,so i think the os should release our heap when it kill
process,isn't it?why i should delete it by myself?
Is there have some problems if i don't delete 'p'?

Thanks for your help.
Oct 10 '06 #3
Osamede Zhang wrote:
I just find i can't understand the code like this
int main()
{
int *p=new int;
//do something
delete p;
return 1;
}
I use new operator allocate some memory in the heap,now i don't need
it,but our process also need to be killed.The heap is in our process'
address space,so i think the os should release our heap when it kill
process,isn't it?
That is up to your operating system.

why i should delete it by myself?
You seem to be the victim of a common misconception, namely that delete
returns memory to the OS. This is not mandated by the standard, and in
fact, it is very often false: many implementations keep memory around for
future allocations by the same program; sometimes the decision whether to
return memory is based upon the size of the chunk. The reason that the
standard is vague about the interaction of delete and the execution
environment (OS) is that the C++ standard cannot legislate the OS to take
the memory back.

However, it is a good habit to delete everything that you new. It helps to
get into that habit. Consider for instance:

class Y {
// some stuff
};

int main ( void ) {
Y* p = new Y;
// do something
delete p;
}

Now it actually makes a tremendous difference whether you call delete p or
not: the destructor of *p is called in one case but not in the other. If
the class Y manages resourced like files, TCP connections or some other
stuff, you may have a resource leak even if the OS reclaims memory.

Is there have some problems if i don't delete 'p'?
In the case above and with an OS that reclaims memory from dead process
bodies: no, since the destructor of int is trivial. However, reread all the
qualifications and ifs in that statement and then put the delete p in just
to get into a good habit.
Best

Kai-Uwe Bux
Oct 10 '06 #4
"Osamede Zhang" <mi************ ***@yahoo.comwr ote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
>I just find i can't understand the code like this
int main()
{
int *p=new int;
//do something
delete p;
return 1;
}
I use new operator allocate some memory in the heap,now i don't need
it,but our process also need to be killed.The heap is in our process'
address space,so i think the os should release our heap when it kill
process,isn't it?why i should delete it by myself?
Is there have some problems if i don't delete 'p'?

Thanks for your help.
Consider, you decide that you don't have to delete p because you are
returning from main, so it works right now. 3 months from now you want to
do the same thing you did here, but in a function.

So you happily open up your old program, copy main into a new program into a
function, delete the things that don't apply, call it from your new programs
main and... wonder why the heck you now have a memory leak.

If you use new, always use delete, even if you don't have to.
Oct 10 '06 #5

The data structure may be very complex,delete them need many time and
even
memory,Is that really worth to do if we don't have to? I really
understand the
type that i use,there are nothing else except some memory in the heap.

Oct 10 '06 #6

Osamede Zhang wrote:
I just find i can't understand the code like this
int main()
{
int *p=new int;
//do something
delete p;
return 1;
}
I use new operator allocate some memory in the heap,now i don't need
it,but our process also need to be killed.The heap is in our process'
address space,so i think the os should release our heap when it kill
process,isn't it?why i should delete it by myself?
Is there have some problems if i don't delete 'p'?

Thanks for your help.
I think, your question is , you allocated some memory which will be
needed whole life time of your programme. why should you free your
memory when u r programme is going down (exiting).
Definately, OS will take the memory when process is exited. Same time,
Freeing memory is good habbit.

Oct 10 '06 #7

Osamede Zhang wrote:
The data structure may be very complex, delete them need many time and
even memory. Is that really worth to do if we don't have to? I really
understand the type that I use, they are nothing else except some
memory in the heap.
If each type is really simple, you can use a pool allocator. In that
case, you
don't need much time to delete the objects. You have a fixed and small
overhead
in the pool allocator.

HTH,
Michiuel Salters

Oct 10 '06 #8
Osamede Zhang wrote:
The data structure may be very complex,delete them need many time and
even
memory,Is that really worth to do if we don't have to? I really
understand the
type that i use,there are nothing else except some memory in the heap.
You should still delete heap allocated objects. Efficiency is only
considered when the program correctness is ensured.

Ben
Oct 10 '06 #9
benben wrote:
Osamede Zhang wrote:
>The data structure may be very complex,delete them need many time and
even
memory,Is that really worth to do if we don't have to? I really
understand the
type that i use,there are nothing else except some memory in the heap.

You should still delete heap allocated objects. Efficiency is only
considered when the program correctness is ensured.
As long as the objects have benign[1] destructors, the correctness of the
program (understood as its input->output transformation) is not in
question: after all IO is done, only UB can get in the way of correctness,
but not deallocating allocated memory is not UB and neither is leaving
objects undestroyed.

What the OP wants is still poor form, but I do not see a correctness issue.
[1] Let's recursively define a benign destructor as a trivial destructor or
a destructor that does nothing but invoking bening destructors. E.g,
disposing of a tree of integers is benign since intergers have trivial
destructors and the nodes only need to delete node pointers and ditch
integer fields. Maybe, I got the technicalities a little wrong, but the
idea is to make sure that destructor calls do not generate any IO at the
end of main.
Best

Kai-Uwe Bux
Oct 10 '06 #10

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

Similar topics

22
1747
by: Alexander Schmolck | last post by:
Two smallish things that have been bugging me; I'm not sure whether they're actually broken or not, but anyway here it goes: 1. ``os.system`` (and co): Shouldn't ``os.system``'s signature really be ``os.system(cmd, arg1, arg2,...)`` rather than ``os.system(some_string)``? Otherwise ``some_string`` almost certainly will be constructed by something along the lines of ``os.system('convert %s %s' % (infile, outfile))`` which of course is...
2
6898
by: shyamal | last post by:
I want to display memory content using C++ on LINUX. For example, the user may ask to display 256 bytes from 0x1000ff00. The problem is , if any location is invalid, the program will coredump because of attempt to access invalid memory.I understand that in Windows OS there is a function that tells if a memory location is valid or invalid. Is there a similar function in Linux? How does gdb display memory content? I will appreciate a...
58
4699
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split (PHONE_LIST, "_+")) { Console.WriteLine (phoneNumber); } } Output: 495.1000
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
5
1970
by: Robin Tucker | last post by:
I've noticed that my program executable remains in the process list if at any stage my program has shown a FolderBrowserDialog. I am calling .Dipose on the dialog after use. This does not happen when I go through the code paths that do not create and display this dialog. Is the folder browser hanging onto something that is holding the rest of my program active on the task list? Any help would be appreciated :) Thanks.
0
687
by: kkk | last post by:
I am practicing LRU by writing a simulating programme in c, but encounter a problem. When obtaining data from a linked list (which stores the requested page number), seemingly the programme falls into an infinite loop. I guess that is because the object obtained through function get() always remains the same. However, I have no idea how to solve it. Would anyone is able to give me an hint? I sincerely appreciate it.
29
2273
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can learn and refresh my memory? :-)
0
1373
by: hennas | last post by:
Basically i want to design a membership Name and Telephone List form using the following command buttons. Edit Add New; Update; Delete; Cancel; Save; Clear, and Exit
4
4112
by: Hong Chen | last post by:
I developed a large program and found the program does not really release memory after the delete operation is taken, since, according to "performance monitor", the virtual bytes used by this process does not decrease after 'delete'. Is this because the heap allocator holds the deleted memory for future use? Is there anyway to force the release of the memory since we confront problem of memory allocation failure if the delete memory is...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.