std::string is *not* defined to be null terminated. There is nothing that
forbids an implementation from doing so, but it is not required. As noted
in a previous response, you must call std::string::c_str() if you need to
guarantee null termination. However, this method can cause a
re-allocation of the string object (which can be an issue in
multi-threaded situations).
It turns out in practice to rarely be important. For instance, if you use
C++ IO operators instead of printf(), null termination is irrelevant. If
you need to iterate over the characters in a string, you can use
std::string::size() or std::string::begin () / end(). All operators on
std::strings work regardless of termination. It is only when interacting
with C interfaces that it matters and the simple solution is to not do
that :-).
At 10:49 AM 4/8/2006, Lynn Allan wrote:
Sorry if this is off-topic.
Just wanted to check .... are
std::string's defined to be null terminated?