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,312 of 33,346    |
|    Seungbeom Kim to wojtek.mula@googlemail.com    |
|    Re: std::string - clear and memory dispo    |
|    17 Apr 14 16:45:36    |
   
   From: musiphil@bawi.org   
      
   On 2014-04-14 12:20, wojtek.mula@googlemail.com wrote:   
   >   
   > Is it legal that method std::string::clear() also shrinks memory   
   > buffer? For example:   
   >   
   > std::string str("long string");   
   > std::cout << str.size() << ", " << str.capacity(); // print: 11, 24   
   > str.clear();   
   > std::cout << str.size() << ", " << str.capacity(); // print: 0, 0   
   >   
   > Popular implementations keeps the buffer (i.e. the fourth line prints   
   > "0, 24"), but I can't find any requirement in the standard.   
      
   I'm not an expert, but I don't see such a requirement, either.   
   All that is required of clear() is equivalence to erase(begin(), end()),   
   and erase(...) doesn't have any further requirements on the capacity.   
      
   My guess is that, for an implementation of clear() simply in terms of   
   erase(begin(), end()), keeping the capacity would be easier and more   
   natural, but other implementations may find it easier just to release   
   the buffer altogether, and the standard seems to support that possibility.   
   (After all, very little is required of the capacity, except that   
   capacity() >= size() and capacity() >= n after reserve(n); even   
   the seemingly explicit shrink_to_fit() is a non-binding request.)   
      
   --   
   Seungbeom Kim   
      
      
    [ 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