Optimal basic_string<>::append

Nathan Myers ncm-nospam@cantrip.org
Mon Dec 17 21:48:00 GMT 2001


On Mon, Dec 17, 2001 at 03:54:04PM +0100, Paolo Carlini wrote:
> first blush, I think this is not ok. The reason is the following: if
> _M_replace_safe, that is _M_mutate inside it, reallocates the string
> (this happen when this != &str) and the string is not shared, the
> iterators __k1 and __k2, pointing to the original memory area, becomes
> invalid (when a string is reallocated because his capacity is exceeded
> (vs because the _Rep becomes unshared) the original memory area is
> freed!)

Paolo, I think I agree with your third analysis.  In general, though,
any operation that makes a string longer than existing capacity permits 
invalidates any iterators.  We should take advantate of every permitted 
opportunity to invalidate iterators because it allows us to share the 
representation on any subsequent copies.

Nathan Myers
ncm at cantrip dot org



More information about the Libstdc++ mailing list