+ 5

Multiple Null Characters in String Literal

For some reason, multiple null ('\0') characters stored in a std::string variable does not reflect when calling length method. std::string str = "\0\0\0"; std::cout << str.length(); // prints 0 but using a different way of building the string: std::string var = std::string(3,'\0'); std::cout << var.length(); // prints 3 Why is this so?

8th Apr 2019, 5:45 AM
Fermi
Fermi - avatar
13 Respostas
+ 5
C strings are null-terminated and C++ strings aren't. In your fist example, you are implicitly casting a `char*`-type string literal to a `std::string`, and, because in a C string, '\0' is considered end-of-string, it won't read any further. But you can still put \0 in a C++ string if you want to, which is what you are doing in the second example.
8th Apr 2019, 6:11 AM
Schindlabua
Schindlabua - avatar
+ 3
Ah I googled and it said in C it's a char* but modifying the contents is UB. I figured it was the same in C++, thanks for clarifying.
8th Apr 2019, 8:42 AM
Schindlabua
Schindlabua - avatar
+ 2
You mean string literals are actually char*, and char*, or string literals in itself, does not store null characters? ... this is just weird, but looks like you are right. It won't even read "\0something". As long as it meets \0, it cuts off. I'd figure that C++ wouldn't interfere with my explicit bidding, but I guess std::string(3, '\0') will work for me.
8th Apr 2019, 6:38 AM
Fermi
Fermi - avatar
+ 2
String literals are char* yeah, but a char* does in fact store the '\0'. The problem is that a char* has no concept of "length" and so you either have to keep track of the length yourself (std::string), or you just keep on reading until you encounter a special value in memory (\0 in C "strings"). When converting between the two you have to be careful. One cool thing you can do with C strings is that you can store many strings in a single char*, by inserting \0s in between. That's what the C function `strtok` does for example!
8th Apr 2019, 7:31 AM
Schindlabua
Schindlabua - avatar
+ 1
@swim Thanks for the tip on raw strings! Very handy!
9th Apr 2019, 12:33 AM
Fermi
Fermi - avatar
+ 1
The first line implicit construct a std::string by detecting length of "\0\0\0", and copy the string into it. The length detection is something like strlen("\0\0\0"), which is same as strlen("\0"), so it construct a zero length std:: string. While the 2nd std:: string is constructed with explicit length.
9th Apr 2019, 12:38 AM
Yi Ren (Rox)
Yi Ren (Rox) - avatar
9th Apr 2019, 6:30 AM
px16
px16 - avatar
+ 1
Fermi If you want to store string literal with null characters in string, you may use: string str("\0something", 9); It is 4th constructor: https://en.cppreference.com/w/cpp/string/basic_string/basic_string Check it in my code. https://code.sololearn.com/cQXsLBx3lLBS/?ref=app
9th Apr 2019, 6:45 AM
px16
px16 - avatar
0
~ swim ~ Raw string cannot contain null character. Your escape sequence "\0" inside raw string are in fact two characters: '\' and '0'. Check it in my code: https://code.sololearn.com/ccYMAiBkNaoS/?ref=app
10th Apr 2019, 8:29 PM
px16
px16 - avatar