Discussion:
std::string - clear and memory dispose
(too old to reply)
w***@googlemail.com
2014-04-14 19:20:07 UTC
Permalink
Hi, this is my first post on this group.

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.

w.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Seungbeom Kim
2014-04-17 23:45:36 UTC
Permalink
Post by w***@googlemail.com
Is it legal that method std::string::clear() also shrinks memory
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! ]
Bo Persson
2014-04-18 15:51:09 UTC
Permalink
Post by Seungbeom Kim
Post by w***@googlemail.com
Is it legal that method std::string::clear() also shrinks memory
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.)
All correct, but with a catch that the conditions after reserve(n) must
hold for an extended period of time. This means that clear() can only
dispose of the buffer if reserve(n) has not been called. No
implementation keeps track of that.

Also, with the small-string-optimization (holding a short string inside
the std::string object - no allocation), 24 might be the minimum
capacity available.


Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Loading...