Search code examples
c++undefined-behavior

Is accessing parts of a string after an embedded null terminator UB?


If I have an embedded null terminator [aside: is that UB?], is it well-defined for me to access the values after it?

#include <stdio.h>

const char foo[] = "abc\0def";
int main() {
  printf("%s", foo+4);
  return sizeof(foo);
}

For the record, it prints what you might expect:

def

Solution

  • An embedded null is NOT Undefined Behavior. It could be a logic error, if you work with functions that expect Strings to be null-terminated. But there's nothing wrong or evil or undefined about accessing the full breadth of an array you've successfully allocated, regardless of its contents.

    One thing to observe though: if you attempt to store this data in a std::string (which is how you should handle all strings, TBH), how you store the string can be important.

    std::string str1 = foo; //contents of str1 becomes "abc".
    std::string str2 = std::string(foo, sizeof(foo)); //contents of str2 becomes "abc\0def"