home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,639 of 33,346   
   Carlos Moreno to All   
   Reference to show that if (this == NULL)   
   07 Nov 11 12:02:34   
   
   From: moreno_news@mailinator.com   
      
   Hi,   
      
   Clarification up-front:  I'm not after a discussion or to explain   
   to me how or why this works or doesn't work.  What I'm really   
   after is a *specific* reference to a section on the standard, or   
   maybe on a book like TC++PL, that *very explicitly* says   
   something about the issue.   
      
   So, the issue:  I'm trying to convince a colleague of mine that   
   using recursion in a tree structure, and allowing it to step outside   
   the tree by calling the method on the NULL pointers is not legal   
   (as in, it invokes undefined behaviour).   
      
   The thing is, it's proving to be an "uphill battle", in that the   
   solution, legal or not, simply happens to work (quite likely works   
   on every compiler that has ever existed --- at least every "real"   
   compiler, as opposed to academic/proof-of-concept compilers),   
   and it simplifies the code a bit.  For example, to compute the   
   sum of all nodes in a tree (say that it's a binary tree of integer   
   values).  So, the solution would go more or less like:   
      
   int Node::sum () const   
   {   
       if (this == NULL)   
       {   
           return 0;    // no data members accessed  (and this is why   
                        // it *happens* to work)   
       }   
       else   
       {   
           return value() + left_node()->sum() + right_node()->sum();   
       }   
   }   
      
   The alternative, though in my opinion is *conceptually* simpler,   
   is, with respect to the code, a bit more cumbersome:   
      
   int Node::sum () const   
   {   
       int total = value();   
       if (left_node() != NULL)   
       {   
           total += left_node()->sum();   
       }   
       if (right_node() != NULL)   
       {   
           total += right_node()->sum();   
       }   
       return total;   
   }   
      
   I guess it could be made more compact with the ternary operator,   
   but still, instead of a single validation for self, we need two   
   validations, one for each pointer.   
      
   Now, this means that we are dereferencing a NULL-pointer, and   
   thus introduces undefined behaviour.   The thing is, I can not find   
   any concrete reference to the standard, or an authoritative book   
   such as TC++PL, that *explicitly* says that this is the case.   
      
   In a thread back from August (subject "Why does this work?", if   
   I remember correctly), many replies (including mine) pointed   
   out that it was undefined behaviour, yet explained why it   
   *happens to work* --- despite one of the replies asking for an   
   explicit statement in the standard that it is undefined behaviour,   
   everyone just *said* it is U.B., but no-one pointed to the *actual*   
   place in the standard where it explicitly says that it is U.B.   
      
   I would be interested also in knowing whether the standard   
   contains statements such as "accessing nonstatic data members   
   through a NULL pointer is undefined behaviour" --- as such a   
   statement would seem to imply that the technique may be legal.   
      
   Again, I'm definitely not interested in a debate over the merits   
   of the technique, or why it would or wouldn't be, or should or   
   shouldn't be, undefined behaviour.  Neither on whether you   
   personally believe that it is or it's not.   (which of course does   
   not prevent you from saying so, or shifting the direction of the   
   thread --- I'm just saying, I'm not interested in such as debate   
   or such replies;  hope I'm not sounding too hostile  :-) )   
      
   Any help, please?   
      
   Thanks,   
      
   Carlos   
   --   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca