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.

timrau
  • 22,578
  • 4
  • 51
  • 64
  • @nucleon Not so. Fixed size text encodings (such as UTF-16) will contain `0x00` bytes when they encode basic ASCII characters. –  May 21 '17 at 10:26
  • 2
    See [this](http://stackoverflow.com/q/2845769/7571258) and [this](http://stackoverflow.com/q/1534335/7571258) and [this](http://stackoverflow.com/q/42653647/7571258). – zett42 May 21 '17 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 – Lightness Races in Orbit May 21 '17 at 11:26
  • @BoundaryImposition It claims it cannot detect the encoding used. I suppose it's a design feature rather than bug. –  May 21 '17 at 11:46
  • @gaazkam: Actually to be fair, since it's supposed to be a _text_ editor, that's probably true. – Lightness Races in Orbit May 21 '17 at 12:26

0 Answers0