Re:Designing a better strcpy
by WalterBright on Saturday June 19, 2021 @09:21PM
(#27566161)
Embedding the length means you cannot slice strings. To get a substring one has to reallocate.
Of course, null termination also means that slicing is restricted to being a tail.
ideally the length should go with the pointer, not with the string.
I feel like to an extent string_view solves that, but in C++ at least you end up having to juggle lifetimes under the hood.
And just generally doing that for all arrays. That way if you're passed ONLY a pointer to the array, you can always tell how long it is without requiring the caller to tell you how long it is, which seems to be a common theme among C functions. And that also allows your string to contain null characters, which is useful in many circumstances.