Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Do I need to check for NULL before delete p?

Reply
Thread Tools

Do I need to check for NULL before delete p?

 
 
sam
Guest
Posts: n/a
 
      07-22-2003

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


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      07-22-2003

"sam" <(E-Mail Removed)> wrote in message newslcTa.255636$(E-Mail Removed) .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).


 
Reply With Quote
 
 
 
 
Paul Thompson
Guest
Posts: n/a
 
      07-22-2003
"sam" <(E-Mail Removed)> wrote in message
newslcTa.255636$(E-Mail Removed) .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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check for NULL before calling free()? Poster Matt C Programming 45 02-06-2010 09:21 AM
To check or not to check for NULL? Mike Java 12 09-26-2008 10:46 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
is (!ptr) or (ptr) valid way to check for NULL or NOT NULL? G Fernandes C Programming 9 02-27-2005 03:07 AM
delete NULL, what about delete[] NULL Mathieu Malaterre C++ 2 08-17-2004 10:02 PM



Advertisments