468,512 Members | 1,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 3663
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Alexander Schmolck | last post: by
2 posts views Thread by shyamal | last post: by
reply views Thread by kkk | last post: by
29 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
4 posts views Thread by Hong Chen | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.