News for hackers. Stuff that matters.
Re:Small Strings in Rust: smolstr vs. smartstring by unclad5968 on Saturday August 24, 2024 @01:16PM (#41339688)
std::string is guaranteed to be null terminated since c++11.

std::string::c_str returns the same address as &string[0]

Re:Small Strings in Rust: smolstr vs. smartstring by dataflow on Saturday August 24, 2024 @01:20PM (#41339728)
> std::string::c_str returns the same address as &string[0]

Note that this by itself doesn't imply null-termination, though as you say strings are indeed null-terminated now.

Edit: This is not particularly obvious, but the reason this has nothing to do with allocation or the return value is that the implementation could still leave space for the null terminator, but avoid actually setting it to zero until c_str() is invoked. That would neither affect the returned pointer nor the constant-time guarantee.

Re:Small Strings in Rust: smolstr vs. smartstring by masspro on Saturday August 24, 2024 @02:00PM (#41340037)
Why downvotes? Ignoring C++11 changes for a minute, c_str is absolutely able to cause a reallocation if it needs to, such that after it returns, its return value and the value of data() are the same. Ergo no, the GP statement alone doesn’t imply anything.
Re:Small Strings in Rust: smolstr vs. smartstring by dpassens on Saturday August 24, 2024 @02:29PM (#41340289)
But the return value guarantee is, as far as I can see, a C++11 change, so you can't ignore those. And as of C++11, c_str is noexcept, so it cannot allocate anything.
Re:Small Strings in Rust: smolstr vs. smartstring by dataflow on Saturday August 24, 2024 @03:08PM (#41340616)
Please see my edit.
Re:Small Strings in Rust: smolstr vs. smartstring by dietr1ch on Saturday August 24, 2024 @09:42PM (#41343606)
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.
Re:Small Strings in Rust: smolstr vs. smartstring by dataflow on Saturday August 24, 2024 @11:48PM (#41344210)
> 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.

Re:Small Strings in Rust: smolstr vs. smartstring by dietr1ch on Sunday August 25, 2024 @04:39AM (#41345614)
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!

Re:Small Strings in Rust: smolstr vs. smartstring by jcelerier on Sunday August 25, 2024 @08:32AM (#41346845)
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.