Is the following snippet of code safe?
No. It is invoking Undefined Behavior.
It invoked std::string's fourth constructor that takes in a pointer to a null terminated string. The thing is, I'm not sure if word below is null terminated. Is it?
No, it is not.
A simple fix, assuming you don't want to null-terminate the array, would be to use a different std::string constructor that takes in the desired length as a parameter, eg:
std::array<char, 4> word{'a', 'b', 'c', 'd'};
int main()
{
std::string p(word.data(), word.size());
// more generally:
// std::string p(word.data(), desired_length);
return 0;
}
Alternatively, you can use a different std::string constructor that takes in iterators instead, eg:
std::array<char, 4> word{'a', 'b', 'c', 'd'};
int main()
{
std::string p(word.begin(), word.end());
// more generally:
// std::string p(word.begin(), word.begin() + desired_length);
return 0;
}
Either way, you don't need a null terminator.