By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,205 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,205 IT Pros & Developers. It's quick & easy.

Do I need to check for NULL before delete p?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"sam" <sa****@nc.rr.com> wrote in message news:ol**********************@twister.southeast.rr .com...

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).
Jul 19 '05 #2

P: n/a
"sam" <sa****@nc.rr.com> wrote in message
news:ol**********************@twister.southeast.rr .com...

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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.