Should I delete these memory before exit process

O

Osamede Zhang

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

mailforpr

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

OrangutanJiang

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

Kai-Uwe Bux

Osamede said:
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
 
J

Jim Langston

Osamede Zhang said:
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.
 
O

Osamede Zhang

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

shaanxxx

Osamede said:
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.
 
M

Michiel.Salters

Osamede said:
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
 
B

benben

Osamede said:
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
 
K

Kai-Uwe Bux

benben said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top