Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the: "std::string can hold '\0' character" by design?

Tags:

c++

standards

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?

like image 926
Šimon Tóth Avatar asked Sep 14 '11 12:09

Šimon Tóth


People also ask

Can std::string contain 0?

Yes you can have embedded nulls in your std::string . Example: std::string s; s. push_back('\0'); s.

Does C++ string end with 0?

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 .

Is std::string zero terminated?

Actually, as of C++11 std::string is guaranteed to be null terminated. Specifically, s[s. size()] will always be '\0' .

What is the type of '\ 0 in C++?

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.


2 Answers

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.

like image 190
pmr Avatar answered Sep 21 '22 19:09

pmr


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.'

like image 27
bmargulies Avatar answered Sep 24 '22 19:09

bmargulies