Comment by dietr1ch
1 year ago
Shouldn't c_str() be null terminated? Then if it must point to the actual backing store, then that must also be a valid null terminated C string as there's no other way to use them.
1 year ago
Shouldn't c_str() be null terminated? Then if it must point to the actual backing store, then that must also be a valid null terminated C string as there's no other way to use them.
> Shouldn't c_str() be null terminated?
Yes, by definition.
> Then if it must point to the actual backing store, then that must also be a valid null terminated C string as there's no other way to use them.
No, this doesn't follow. The buffer could leave room for the terminator but not actually write the terminator until c_str() is called. That's why the C++11 standard had to explicitly require that the string always be null-terminated regardless of whether c_str() is called.
Wow, I didn't expect `c_str()` to write at all. I knew that C++ was wild before C++57, but before C++ is insane.
Thanks for the explanation!
I don't think any implementation ever did this. But it was "technically possible" as the spec didn't prevent it so you have to account for it.