473,569 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1985
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************ *************** *******@k13g200 0hse.googlegrou ps.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.comn et>,
Alf P. Steinbach <al***@start.no wrote:
>* 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
12970
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' Effective STL), and reads like this: void CGame::RemoveDeadObjects() { // cleanup crew!! vector<CDrawableObject*>::iterator i;
15
3579
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
1644
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 I am trying to access freed. memory. Is my assumption correct? I tried following code ---------------------------------------------------...
9
1871
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. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather...
51
10258
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
10
10219
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
17675
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(); img.src = ; Then I use the image a few more times by adding it into an Array object:
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.