deleting a null pointer

M

ma740988

Given the snippet.

class foo {
public:
~foo() {
// type id should be able to get the type of the class ( I think )
std::cout << " foo destructing " << std::endl;
}
};


int main() {

foo *ptr = new foo () ;
delete ptr;

foo *ptr_ ( 0 );
if ( ptr_ ) {
delete ptr_ ;
}
}

If memory serves the check "if ( ptr_ )" is NEVER (UN) necessary.
Trouble is, I don't recall the impetus surrounding why or where in the
standard I found this. My standard is not within arms reach that
said, the question: Could I get confirmation on that deleting a void
pointer is indeed valid and where (source) in the standard I could
confirm this?

Thanks alot
 
M

ma740988

Victor, Greg as always, thanka alot. One last item on this. There's
a common guideline - if you will - that suggests
if ( ptr_ )
delete ptr_ ;

I'm trying to understand why would I _ever_ do that? Thus far the
only scenario I could concoct surrounds:


delete ptr_ ; // item 1
// in some other translation unit.. later I do
delete ptr_ ; // item 2

item 1: Ok there.
item 2: I suspect here I could solve a potential issues with an 'if
( ptr_ )'. Trouble, is, if I opt to delete a pointer twice, then I'm a
mook, something is amiss in the code.

Oh well, it's a guideline though I might add I find it (literally)
drives me insane wnen I encounter it.
 
B

Bo Persson

ma740988 said:
Victor, Greg as always, thanka alot. One last item on this.
There's a common guideline - if you will - that suggests
if ( ptr_ )
delete ptr_ ;

I'm trying to understand why would I _ever_ do that?

You can find this in old code, from the time before there was a C or
C++ standard. At the time, various compilers had different ideas about
what delete NULL meant.

This is one reason for creating a standard! .-)


Bo Persson
 
G

Gennaro Prota

ma740988 said:
Victor, Greg as always, thanka alot. One last item on this. There's
a common guideline - if you will - that suggests
if ( ptr_ )
delete ptr_ ;

I'm trying to understand why would I _ever_ do that? Thus far the
only scenario I could concoct surrounds:


delete ptr_ ; // item 1
// in some other translation unit.. later I do
delete ptr_ ; // item 2

item 1: Ok there.
item 2: I suspect here I could solve a potential issues with an 'if
( ptr_ )'.

Nope: evaluating ptr in the if invokes undefined behavior, just like
double deletion would do. You can set the pointer to null after every
delete (in which case, of course, you have no UB evaluating the if
condition later), but then the point you make immediately below holds.
(In any case, you can't do anything --except proper design-- against
something like

ptr2 = ptr;
...
delete ptr;
ptr = 0;
...
delete ptr2;
ptr2 = 0;
)
Trouble, is, if I opt to delete a pointer twice, then I'm a
mook, something is amiss in the code.

Right. The code misses proper design.
 

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,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top