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 10 of 16 Topic 9494 of 9983
Post > Topic >>

Re: Avoiding dangling pointers.

by Jiri Palecek <jpalecek@[EMAIL PROTECTED] > Apr 12, 2008 at 12:13 PM

Rafael Anschau wrote:

> I read that you should assign null (0) to all pointers that you call
> delete on.
> 
> Does that mean:
> 
> *p=0(set the value pointed to to 0).
> 
> or
> 
> p=0(set the address held to zero).

This one.

> The last one is awkward for none would want a pointer pointing
> to address zero(even though it compiles fine, you canīt assign
> anything to it).

The rationale is that a null pointer is much easier to debug than a
dangling
pointer. When you access a null pointer, on most platforms it send the
program to death (or debugger), but using a dangling pointer can overwrite
someone else's data which can manifest itself much much later. You should
certainly do it if you know you're gonna read the pointer later.

But don't take the advice (about setting pointers to zero) too seriously.
Basically, if your program has the memory management stuff correct (which
it really, really should), it is just superfluous. If you've deleted an
object through one of the ten pointers to it, the other nine are still
left
dangling. Also, if you use valgrind to debug your code, it will show you
detailed info about accesses through dangling pointers (like backtraces
from the time it was allocated and freed), but not if you set it to 0.

It's like uninitialised variables: you can always get rid of uninitialised
variable accesses by initialising every variable to 0, but it's up to you
to decide for a particular variable whether it makes sense or it's just
hiding a bug.

Regards
     Jiri Palecek


-- 
      [ 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 Sun Sep 7 4:04:09 CDT 2008.