This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/52938] std::string::reserve request is not maintained if object is used in other object copy ctore
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 11 Apr 2012 20:57:18 +0000
- Subject: [Bug libstdc++/52938] std::string::reserve request is not maintained if object is used in other object copy ctore
- Auto-submitted: auto-generated
- References: <bug-52938-4@http.gcc.gnu.org/bugzilla/>
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.