Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Avoiding da...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 15 of 16 Topic 9494 of 9971
Post > Topic >>

Re: Avoiding dangling pointers.

by Alan McKenney <alan_mckenney1@[EMAIL PROTECTED] > May 5, 2008 at 07:08 AM

On Apr 11, 5:33 pm, Rafael Anschau <rafael.ansc...@[EMAIL PROTECTED]
> wrote:
> I read that you should assign null (0) to all pointers that you call
> delete on.

I would describe this advice as "Cargo Cult
Programming."  While there are situations where
you will want to set a point to zero after a
delete, there are far more situations where
it is just wasted code.

The real way to avoid dangling pointers is
to figure out _why_ you are getting dangling
pointers, and deal with _that_.  A dangling
pointer may indicate that your code is confused
about who owns the object, or that you have
not thought out the object's lifetime or
life cycle.  Or that you haven't thought
out your use cases (so you're calling a function
that expects a pointer in a situation in which
there is logically nothing to point to.)
Or a host of other logic errors.

By the way, while I think smart pointers are
a great idea, I'm a little leery of advising
beginners to use them to avoid pointer problems.
IMHO, most of the time, pointer problems arise
because of logic errors which can't be fixed
by substituting smart pointers.

There's really no substitute for knowing what
you're doing, and it's doubly so when dealing
with pointers (dumb or smart.)


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 16 Posts in Topic:
Avoiding dangling pointers.
Rafael Anschau <rafael  2008-04-11 15:33:08 
Re: Avoiding dangling pointers.
d04rp@[EMAIL PROTECTED]   2008-04-11 18:06:51 
Re: Avoiding dangling pointers.
Sean Hunt <rideau3@[EM  2008-04-11 18:08:47 
Re: Avoiding dangling pointers.
Francis Glassborow <fr  2008-04-11 18:11:05 
Re: Avoiding dangling pointers.
Rafael Anschau <rafael  2008-04-12 12:08:56 
Re: Avoiding dangling pointers.
"Alf P. Steinbach&qu  2008-04-12 12:09:58 
Re: Avoiding dangling pointers.
Carl Barron <cbarron41  2008-04-12 12:07:12 
Re: Avoiding dangling pointers.
pstallworth@[EMAIL PROTEC  2008-04-12 12:13:11 
Re: Avoiding dangling pointers.
Thomas Maeder <maeder@  2008-04-12 12:13:10 
Re: Avoiding dangling pointers.
Jiri Palecek <jpalecek  2008-04-12 12:13:11 
Re: Avoiding dangling pointers.
Ulrich Eckhardt <eckha  2008-04-14 12:42:42 
Re: Avoiding dangling pointers.
galathaea <galathaea@[  2008-04-14 21:25:34 
Re: Avoiding dangling pointers.
Michiel.Salters@[EMAIL PR  2008-05-02 12:46:39 
Re: Avoiding dangling pointers.
"Andrew Koenig"  2008-05-04 17:16:05 
Re: Avoiding dangling pointers.
Alan McKenney <alan_mc  2008-05-05 07:08:52 
Re: Avoiding dangling pointers.
Andy Champ <no.way@[EM  2008-05-06 18:21:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:49:42 CDT 2008.