3

Consider the following code:

#include <iostream> #include <string> using namespace std; int main() { string s = "Allie has a cat."; s[3] = '\0'; cout << s << '\n'; return 0; } 

Is this code valid? Are there any pitfalls that would make it invalid if in addition to what is above we would do some other operations on this string?

  • In C, the '\0' character was used as a string terminator. std::string still seems to follow this tradition. THerefore, it wouldn't be unreasonable to suppose that under certain circumstances a premature '\0' might break methods like std::string::size().
  • It wouldn't be unreasonable to suppose that some routines like cout might use the underlying std::string::c_str() for their operation, perhaps entering some sort of UB when this null-terminated string is "prematurely terminated".

But suppose that, as on ideone, couting such a std::string works fine and prints out this:

Alle has a cat. 

I’ve compiled and run this program locally and confirmed that it does print out 0x00 in between of l and e. Is it valid to print out null characters to the console? Is it valid for text files to contain null characters? gedit refuses to open the file to which this program's output has been redirected.

5
  • @nucleon Not so. Fixed size text encodings (such as UTF-16) will contain 0x00 bytes when they encode basic ASCII characters. Commented May 21, 2017 at 10:26
  • 2
    See this and this and this. Commented May 21, 2017 at 10:28
  • "gedit refuses to open the file to which this program's output has been redirected" Then it is deeply buggy. If your problem description went beyond "gedit refuses" then maybe we'd be able to help Commented May 21, 2017 at 11:26
  • @BoundaryImposition It claims it cannot detect the encoding used. I suppose it's a design feature rather than bug. Commented May 21, 2017 at 11:46
  • @gaazkam: Actually to be fair, since it's supposed to be a text editor, that's probably true. Commented May 21, 2017 at 12:26

0