Does std::string have a null terminator?

C++StringNull

C++ Problem Overview


Will the below string contain the null terminator '\0'?

std::string temp = "hello whats up";

C++ Solutions


Solution 1 - C++

No, but if you say temp.c_str() a null terminator will be included in the return from this method.

It's also worth saying that you can include a null character in a string just like any other character.

string s("hello");
cout << s.size() << ' ';
s[1] = '\0';
cout << s.size() << '\n';

prints

5 5

and not 5 1 as you might expect if null characters had a special meaning for strings.

Solution 2 - C++

Not in C++03, and it's not even guaranteed before C++11 that in a C++ std::string is continuous in memory. Only C strings (char arrays which are intended for storing strings) had the null terminator.

In C++11 and later, mystring.c_str() is equivalent to mystring.data() is equivalent to &mystring[0], and mystring[mystring.size()] is guaranteed to be '\0'.

In C++17 and later, mystring.data() also provides an overload that returns a non-const pointer to the string's contents, while mystring.c_str() only provides a const-qualified pointer.

Solution 3 - C++

This depends on your definition of 'contain' here. In

std::string temp = "hello whats up";

there are few things to note:

  • temp.size() will return the number of characters from first h to last p (both inclusive)
  • But at the same time temp.c_str() or temp.data() will return with a null terminator
  • Or in other words int(temp[temp.size()]) will be zero

I know, I sound similar to some of the answers here but I want to point out that size of std::string in C++ is maintained separately and it is not like in C where you keep counting unless you find the first null terminator.

To add, the story would be a little different if your string literal contains embedded \0. In this case, the construction of std::string stops at first null character, as following:

std::string s1 = "ab\0\0cd";   // s1 contains "ab",       using string literal
std::string s2{"ab\0\0cd", 6}; // s2 contains "ab\0\0cd", using different ctr
std::string s3 = "ab\0\0cd"s;  // s3 contains "ab\0\0cd", using ""s operator

References:

Solution 4 - C++

Yes if you call temp.c_str(), then it will return null-terminated c-string.

However, the actual data stored in the object temp may not be null-terminated, but it doesn't matter and shouldn't matter to the programmer, because when then programmer wants const char*, he would call c_str() on the object, which is guaranteed to return null-terminated string.

Solution 5 - C++

With C++ strings you don't have to worry about that, and it's possibly dependent of the implementation.

Using temp.c_str() you get a C representation of the string, which will definitely contain the \0 char. Other than that, i don't really see how it would be useful on a C++ string

Solution 6 - C++

std::string internally keeps a count of the number of characters. Internally it works using this count. Like others have said, when you need the string for display or whatever reason, you can its c_str() method which will give you the string with the null terminator at the end.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmisterView Question on Stackoverflow
Solution 1 - C++jahhajView Answer on Stackoverflow
Solution 2 - C++user529758View Answer on Stackoverflow
Solution 3 - C++aniliitb10View Answer on Stackoverflow
Solution 4 - C++NawazView Answer on Stackoverflow
Solution 5 - C++Naps62View Answer on Stackoverflow
Solution 6 - C++SupermanView Answer on Stackoverflow