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 32,059 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Modification of trailing null charac   
   28 Mar 12 12:13:36   
   
   f0bfee87   
   From: daniel.kruegler@googlemail.com   
      
   {Please do not overquote -mod/we}   
      
   Am 28.03.2012 19:55, schrieb Nikolay Ivchenkov:   
   > According to N3376 - 21.4.5/2   
   >   
   >      reference operator[](size_type pos);   
   >   
   >      Returns: *(begin() + pos) if pos<  size(). Otherwise, returns a   
   >      reference to an object of type charT with value charT(), where   
   >      modifying the object leads to undefined behavior.   
   >   
   > If my understanding of this rule is correct, we can't safely assign   
   > zero value to the trailing null character in a string object, even   
   > if the string is non-empty:   
   >   
   > 1)   
   >      std::string s;   
   >      char&c = s[0];    // well-defined   
   >      assert(c == '\0'); // succeeds   
   >      c = '\0';          // undefined   
   > 2)   
   >      std::string s = "text";   
   >      char&c = s[s.size()]; // well-defined   
   >      assert(c == '\0');     // succeeds   
   >      c = '\0';              // undefined   
      
   This interpretation is correct, yes.   
      
   > and the following implementation of get_window_text_w is not   
   > well-defined:   
   >   
   >      #include   
   >      #include   
   >   
   >      std::wstring get_window_text_w(HWND hwnd)   
   >      {   
   >          std::wstring result;   
   >          result.resize(GetWindowTextLengthW(hwnd));   
   >          GetWindowTextW(hwnd,&result[0], result.size() + 1);   
   >          return result;   
   >      }   
   >   
   > (here GetWindowTextW assigns zero value to the trailing null   
   > character), so we need to use extra character for such C API calls:   
   >   
   >      std::wstring get_window_text_w(HWND hwnd)   
   >      {   
   >          std::wstring result;   
   >          result.resize(GetWindowTextLengthW(hwnd) + 1);   
   >          GetWindowTextW(hwnd,&result[0], result.size());   
   >          result.pop_back();   
   >          return result;   
   >      }   
   >   
   > Did I miss something?   
      
   No, the current wording is intended to have these (undefined) effects. A   
   valid implementation could e.g. use the same constant char object as a   
   common null character for all empty strings.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
   --   
         [ 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