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

Re: Avoiding dangling pointers.

by Ulrich Eckhardt <eckhardt@[EMAIL PROTECTED] > Apr 14, 2008 at 12:42 PM

Rafael Anschau wrote:
> If you set p=0, you are not pointing nowhere, and I found that weird.
But
> it actualy makes sense, it´s better to have the program crash
> right away if something tries to write on it, than to have it write fine
> on some dark zone of heapland which you will never be able to reach
again.
> Reduces debugging time.

Actually, you missed a point: if the value of a pointer is zero, this is a
special signal value which means that it doesn't point to an object. Of
course, trying to use the object it points to (which doesn't exist) will
result in a crash, but even more im****tant is the fact that you can
_check_
if the pointer is zero and thus prevent the crash!

int* i = new int(42);
delete i;
/* 'i' now points to nowhere ('dangling pointer'), but there is no way to
detect that it doesn't point to an object. */
i = 0;
// Now you again have a usable state that you can work with


Note that a good way to avoid dangling pointers is to use so-called 'smart
pointers', which make reasonable sure that they are never dangling and
also
achieve defined error signalling behaviour even in the face of typical
programming errors.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932


      [ 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 3:57:44 CDT 2008.