[RFC] First 3.5 project: don't assign _S_terminal in string::c_str()
Paolo Carlini
pcarlini@suse.de
Sat Jan 17 13:35:00 GMT 2004
Hi,
in the current basic_string implementation we are quite careful
in keeping the internal representation null-terminated: I have
spotted only one place which should be fixed: _M_clone.
So, I would like to fix that and then remove the assignment:
const _CharT*
c_str() const
{
// MT: This assumes concurrent writes are OK.
const size_type __n = this->size(); <=====
traits_type::assign(_M_data()[__n], _Rep::_S_terminal); <=====
return _M_data();
}
This is a bit risky (-> a tiny purely mainline project), because
if we do something wrong the user may call c_str() and have back
a non-null-terminated entity, completely unusable as a C-string.
On the other hand, removing the assign makes c_str way faster.
I have already checked that apparently we are already doing the
right thing for empty strings too, thanks to this:
// The following storage is init'd to 0 by the linker, resulting
// (carefully) in an empty string with one reference.
static size_type _S_empty_rep_storage[];
Paolo.
More information about the Libstdc++
mailing list