Do I need to check for NULL before delete p?

S

sam

For Example, If I delete two times like the below code, the program will
behave differently. is this correct?
As C++ FAQ the delete checks where or not f1 has memory. How can the
following program can go wrong?

class foo{
//
};
int main{
foo *f1 = new foo()
delete f1;
delete f1;
return (0);
}


C++ FAQ
[16.7] Do I need to check for NULL before delete p?

No!

The C++ language guarantees that delete p will do nothing if p is equal to
NULL. Since you might get the test backwards, and since most testing
methodologies force you to explicitly test every branch point, you should
not put in the redundant if test.

Wrong:

if (p != NULL)
delete p;

Right:

delete p;


Thanks
Sam
 
R

Ron Natalie

sam said:
For Example, If I delete two times like the below code, the program will
behave differently. is this correct?

It is undefined behavior.
As C++ FAQ the delete checks where or not f1 has memory. How can the
following program can go wrong?

The FAQ doesn't say that, and your program is wrong.
[16.7] Do I need to check for NULL before delete p?

No!

The C++ language guarantees that delete p will do nothing if p is equal to
NULL. Since you might get the test backwards, and since most testing
methodologies force you to explicitly test every branch point, you should
not put in the redundant if test.

Delete only checks to see if the value passed in is NULL. Otherwise it
needs to be a previous new'd value (not already deleted).

In your case you feed the same, non-NULL value to delete twice.
This is undefined behavior. Don't do it.

If you had set f1 to null

int main{
foo *f1 = new foo()
delete f1;
f1 = 0;
delete f1;
return (0);
}

Then it would have been ok (the latter delete would be a no op).
 
P

Paul Thompson

sam said:
For Example, If I delete two times like the below code, the program will
behave differently. is this correct?
As C++ FAQ the delete checks where or not f1 has memory. How can the
following program can go wrong?

class foo{
//
};
int main{
foo *f1 = new foo()
delete f1;
delete f1;
return (0);
}


When you delete a pointer, the pointer itself is not changed - it still
points where it used to, but what it points at isn't there any more.

In your code, you free the foo object pointed to by f1, and then try to free
the object again. Since the object is no longer there, anything might
happen.

C++ FAQ
[16.7] Do I need to check for NULL before delete p?

No!

The C++ language guarantees that delete p will do nothing if p is equal to
NULL. Since you might get the test backwards, and since most testing
methodologies force you to explicitly test every branch point, you should
not put in the redundant if test.

Wrong:

if (p != NULL)
delete p;

Right:

delete p;

Paul
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top