473,425 Members | 1,854 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,425 software developers and data experts.

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 3899
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.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.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
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...
2
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...
58
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...
11
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...
5
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...
0
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...
29
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...
0
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.