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 12 of 16 Topic 9494 of 10094
Post > Topic >>

Re: Avoiding dangling pointers.

by galathaea <galathaea@[EMAIL PROTECTED] > Apr 14, 2008 at 09:25 PM

On Apr 11, 2: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.
>
> Does that mean:
>
> *p=0(set the value pointed to to 0).
>
> or
>
> p=0(set the address held to zero).
>
> 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).
>
> I find the first still awkward(though less than the seccond) for I
> would assume you shouldnīt
> assign values to places you no longer use.

i would say both methods are suboptimal

the point for this idiom
   is that behavior must change depending on state
   (presence or absence of pointer means different actions needed)

in c++
   this should be relegated to RAII preserving state machines
   and the only (smart) pointers dealt with
     should be the pointers to current state

one of the earliest
(and still one of the most im****tant)
   language features that made c++ a "better c"
is RAII and the ability to set up
automated state entry and leaving actions
   (ctors and dtors)

lifetime management should always fall into one of four cl*****:
   - tem****ary / anonymous / expression
   - block (automatic) scope
   - a state lifetime
   - global / application

(one could make another distinction on message lifetimes
    since it is common to need to pass messages across thread
boundaries
    but discard them once processing is done
  but here the message queues play the role of state machines
  both informally and purely formally)

the only one that requires heap allocation is state lifetime
   which should be created by some transition event on entry
   and deleted by exiting the state

the state objects should contain anything with the lifetime of the
state
   and thus can always assume their presence

this fixes all problems with zombie objects
and separates out behaviors to only work with valid memory

using
   if (p) doSomethingWithP(p);
everywhere p is used
is exactly the same as
   if (inStateX) doSomethingRelatedToStateX();

ie.
   - it is error prone
   - leaving actions are not automated
   - it is error prone
   - it is procedural and interleaves behaviors
and
   - it is error prone

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
galathaea: prankster, fablist, magician, liar


-- 
      [ 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 Sat Oct 11 15:08:44 CDT 2008.