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,825 of 33,346   
   Pete Becker to Robin Morris   
   Re: Calling c_str directly from a functi   
   21 Jan 12 06:23:57   
   
   21330450   
   From: pete@versatilecoding.com   
      
   On 2012-01-20 22:57:30 +0000, Robin Morris said:   
      
   >   
   > int main() {   
   > 	old_c_function(const_cast(getstring().c_str()))   
   > }   
   >   
   > This code works, and valgrind does not complain about it, but Is there   
   > any chance that the  pointer passed to old_c_function could be   
   > invalidated before old_c_function is done with it?   
      
   No problem here. getstring() returns a temporary object, and that   
   temporary exists until the end of the full expression that it was   
   created in. The c-style string returned by c_str() also exists until   
   the end of the full expression. The full expression here ends at the   
   semi-colon that is missing from the end of the line, i.e., it exists   
   until old_c_function returns.   
      
   --   
     Pete   
   Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The   
   Standard C++ Library Extensions: a Tutorial and Reference   
   (www.petebecker.com/tr1book)   
      
      
         [ 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