double free

T

thomas

foo *p = new foo();
----------
1) if(p!=NULL) delete p;
2) delete p; p = NULL;
3) delete p; delete p;
----------------------code-------------------

Double free can be a disaster in the case of 3).
To avoid "double free", it's better to set the pointer NULL when
released.

Since C++ guarantees "NULL pointer" is not handled, 1) is not
necessary.

My question is whether C++ gurantees the pointer is reset to "NULL"
after the memory is freed?
Or whether the expression "p=NULL;" in case 2) necessary?
 
N

Nick Keighley

foo *p = new foo();
----------
1)   if(p!=NULL) delete p;
2)   delete p;   p = NULL;
3)   delete p;   delete p;
----------------------code-------------------

Double free can be a disaster in the case of 3).
To avoid "double free", it's better to set the pointer NULL when
released.

This is debatable. It doesn't help if you have more than one pointer
to the same memory block. As with most C++ problems the answer is
"invent another class" :)

You shouldn't really have "naked" pointers to dynamic memory lying
around in your code. Hide it in an object that manages it for you.

Since C++ guarantees "NULL pointer" is not handled, 1) is not
necessary.

My question is whether C++ gurantees the pointer is reset to "NULL"
after the memory is freed?
nope.

Or whether the expression "p=NULL;" in case 2) necessary?

better to have a clear idea of the lifetime of your object
 
B

Balog Pal

thomas said:
foo *p = new foo();
----------
1) if(p!=NULL) delete p;
2) delete p; p = NULL;
3) delete p; delete p;
----------------------code-------------------

Double free can be a disaster in the case of 3).
To avoid "double free", it's better to set the pointer NULL when
released.

Better than nothing. But way worse than going the idiomtic route: make
'delete' a frobidden keyword for the "user code". RRID/RAII solves this
problem while also making the code more correct and readable in other areas
as well.

Is there any practical problem to immediately put the result of new in a
smart pointer or other manager? In that small portion of code new is still
needed, as the bulk is fine with using locals and collections.
Since C++ guarantees "NULL pointer" is not handled, 1) is not
necessary.
Yeah.

My question is whether C++ gurantees the pointer is reset to "NULL"
after the memory is freed?

Certainly not. A regular pointer is just an object in its own right, it is
set by the programmer's commands. However the smart pointers are invented
for this exact reason, .reset() ensures the memory is disposed and the
pointer's state goes empty.

Dumb pointers shall not be used as owners for a set of reasons -- i.e.
exceptions will leave you with a leak.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top