Boost logo

Boost Users :

From: Alan M. Carroll (amc_at_[hidden])
Date: 2006-04-08 12:15:12


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?

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net