http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52938

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-11 
20:57:18 UTC ---
(In reply to comment #2)
> Why do I see memory is being reallocated even though the string x content can
> fit within what I requested in my reserve call?

Because GCC's std::string uses copy-on-write, so the original memory is shared
between x and y.  When x is modified that memory must be un-shared (i.e. make a
copy of the shared data before doing a write to x).  It's not possible to alter
y when assigning to x, so x must be altered.

I suppose it would be possible to ensure that when x copies the data it copies
the full capacity, but I  don't think we want to change std::string in that
way, so I suggest this should be closed WONTFIX.

Reply via email to