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 33,130 of 33,346   
   Ike Naar to kawninzx6@googlemail.com   
   Re: dynamic_cast undefined behavior   
   20 Jul 13 00:23:51   
   
   From: ike@ukato.freeshell.org   
      
   On 2013-07-20, kawninzx6@googlemail.com  wrote:   
   > class BaseFoo   
   > {   
   >   public:   
   >     virtual ~BaseFoo() {}   
   > };   
   >   
   > class Foo : public BaseFoo {};   
   >   
   > void process(int& i) { ... }   
   >   
   > void process(Foo& foo) { ... }   
   >   
   > BaseFoo* getFoo() { ... }   
   >   
   > int main()   
   > {   
   >   int *i = NULL;   
   >   void process(*i); // (1)   
   >   
   >   try   
   >   {   
   >     process(dynamic_cast(*getFoo())); // (2)   
   >   }   
   >   catch (const std::exception&)   
   >   {   
   >     // ignore bad_cast exception   
   >   }   
   >   
   >   return 0;   
   > }   
   >   
   > (1) - This is always undefined behavior, right?   
      
   Yes, dereferencing a null pointer produces undefined behaviour.   
      
   > (2) - Does this dynamic_cast produce undefined behavior if   
   >       getFoo() returns a NULL pointer?   
      
   It's not the dynamic_cast that produces undefined behaviour,   
   it's the dereference of the null pointer returned by getFoo().   
      
      
   --   
         [ 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