473,395 Members | 2,151 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,395 software developers and data experts.

deleting a null pointer

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
Jul 28 '08 #1
6 1975
ma740988 wrote:
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?
[expr.delete]/2 :
"In either alternative {delete or delete[] -- vb}, if the value of the
operand of delete is the null pointer the operation has no effect."

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '08 #2
In article <6f**********************************@k13g2000hse. googlegroups.com>,
ma740988 <ma******@gmail.comwrote:
>Could I get confirmation on that deleting a void pointer
....null pointer...
>is indeed valid and where (source) in the standard I could
confirm this?
$12.4p12 and $3.7.3.2p3 help but the exact refernce is $5.3.5p2
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 28 '08 #3
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.
Jul 28 '08 #4
ma740988 wrote:
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
Jul 29 '08 #5
In article <3P******************************@posted.comnet> ,
Alf P. Steinbach <al***@start.nowrote:
>* 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?

The 'if' will always be redundant.
> 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_ )'.

No, you could not.

Anyway that 'if' is performed by 'delete'.
>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.

Good. Because that means you still have a working mind. :-)
As opposed to those writing the guideline.
Once upon a time, in a galaxy far far away, it was actually
unclear what delete of a null pointer did, same as free()ing
a null pointer -- different implementations did different things.
So the original intents of some of these rules makes sense.
On the on side of the coin, consistency in coding guidelines
makes sense, but of course sometimes logic defies itself.
Also, there are some who have determined that for their own
code that the if is faster in some critical code, although
I never quite totally bought that argument even if they
could show me their (no doubt correct) timing results.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 29 '08 #6
ma740988 wrote:
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.

--
Gennaro Prota | <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Jul 29 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Hanzo | last post by:
I'm iterating over a vector of base class pointers and deleting those which meet a certain criteria...i'm using pretty text-book code for the particular delete/erasure (it's straight out of Myers'...
15
by: Rick | last post by:
Hi, Does deleting an object more than one times incur undefined behavior? I think it doesn't but just making sure... thanks Rick
6
by: Abhijeet | last post by:
I was just toying around idea of deleting this from a member function. Was expecting that any acess to member variable or function after deleting sould give me dump(segmetation violation).Cause now...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
10
by: Daniel Vukadinovic | last post by:
OK, here's the deal. I'd like to delete the list.Here's what I do: node* p_temp = p_beginning; while (p_beginning != 0) { p_beginning = p_beginning->p_next; delete p_beginning; }
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.