473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Do I need to check for NULL before delete p?

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
2 13862

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patrick Gunia | last post by:
Hi, i´m trying to build a xml - parser, which should simply list all used tokens an dattributes including their values. So far, so good, this works, but now i try to check for illegal phrases in...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
1
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
7
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
1
by: mohammedss | last post by:
Hi everybody I'm a new user for mysql,i'v an application running mysql db on a linux server , now i need to check the modification time of a table and compare with a previous stored one and if...
4
by: rosh72851 | last post by:
Header file #ifndef STRING_HPP #define STRING_HPP #include <iostream> using namespace std; /* mystring class */
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.