Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How do null characters work in std::string?

Tags:

c++

string

I come from C background, while learning C++ I came across the <string> header file. In C strings would be an array of characters terminated by '\0'.

However, in std::string I found that this is not the case, and on inserting/replacing a null character at any valid index does not trim the string as I would have expected.

string s;

getline(cin, s);

// remove all punctuation 
for(string::size_type i = 0, n = s.size(); i < n; i++)
{
     if(ispunct(s[i]))
         s[i] = '\0';
}

input: Hello, World!!!!

output: Hello World

expected output: Hello

On observing the above behaviour I assumed that strings in C++ are not null terminated. Then I found this question on SO Use of null character in strings (C++) This got me confused.

string s = "Hello\0, World";

cout << s << endl;

output: Hello

expected output: Hello, World

It would be helpful if anyone could explain the reason behind this behaviour.

like image 448
CaptainDaVinci Avatar asked Dec 23 '22 20:12

CaptainDaVinci


1 Answers

std::string supports embedded NUL characters*. The fact that your example code doesn't produce the expected result is, because you are constructing a std::string from a pointer to a zero-terminated string. There is no length information, and the c'tor stops at the first NUL character. s contains Hello, hence the output.

If you want to construct a std::string with an embedded NUL character, you have to use a c'tor that takes an explicit length argument:

std::string s("Hello\0, World", 13);
std::cout << s << std::endl;

produces this output:

Hello, World


* std::string maintains an explicit length member, so it doesn't need to reserve a character to act as the end-of-string sentinel.
like image 82
IInspectable Avatar answered Jan 03 '23 01:01

IInspectable