The fact that std::string
can actually hold '\0'
characters comes up all the time. This is of course inconsistent with C-style strings.
So I'm wondering, is this by design, or is it an omission, or is it just the fact that standard doesn't forbid it and compilers allow this to happen?
Yes you can have embedded nulls in your std::string . Example: std::string s; s. push_back('\0'); s.
Note that C++ std::string are not \0 terminated, but the class provides functions to fetch the underlying string data as \0 terminated c-style string. In C a string is collection of characters. This collection usually ends with a \0 .
Actually, as of C++11 std::string is guaranteed to be null terminated. Specifically, s[s. size()] will always be '\0' .
The \0 is treated as NULL Character. It is used to mark the end of the string in C. In C, string is a pointer pointing to array of characters with \0 at the end.
I'm wondering what your quarrel is. '\0'
is just another character. There is no efficient way to forbid it in a general purpose 'char' string.
That the same character has a special meaning in C is unfortunate but has to be dealt with as every restriction that is imposed by legacy code as soon as you interoperate with it.
This shouldn't be an issue as long as you stick to code that uses std::string
exclusively.
To address your comment we need to look at the constructor that takes a char*
which would be basic_string(const charT* s, const Allocator& a = Allocator())
in 21.4.2 9/10
in n3242. It says that the size of the internal string is determined through traits::length(s)
which in the case of std::string
is strlen
which requires its argument to be null terminated. So yes, if you try to construct a std::string
from an const char*
it needs to be null terminated.
There is a set of functions that accept 'char *' arguments and assume that the string is terminated by a zero. If you use them carefully, you can certainly have strings with 0's in them.
STL strings, in contrast, intentionally permit zero bytes, since they don't use 0 for termination. So the simple answer to your question is, 'yes, by design.'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With