std::string::reserve()

Neil Ferguson nferguso@eso.org
Wed Feb 11 21:07:00 GMT 2004


Hi, all -

I noticed earlier that calling std::string::reserve() in a
shrink-to-fit request can sometimes end up allocating exactly
the same amount of memory as the string already had.

This is because _S_create() rounds up allocations of over 128
bytes to 128 byte subpages, and of over 4k to 4k pages, for
more efficient use of malloc().

When the reallocated block is the same size as the original
one, the entire contents of the string are effectively being
copied unnecessarily. Further shrink-to-fit calls keep doing
it, too, since the requested capacity remains less than the
actual capacity.

I've attached diffs against latest CVS that try to avoid doing
this, and I'd be interested to know what you think.

Neil.

PS. The diffs seem to work with a small test program, but I
     can't get the dejagnu test suite to work right now (lots
     of cc1plus segfaults?) so please treat with caution...
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: basic_string.h.diff
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20040211/40f2f1fd/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: basic_string.tcc.diff
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20040211/40f2f1fd/attachment-0001.ksh>


More information about the Libstdc++ mailing list