News for hackers. Stuff that matters.
Re:Designing a better strcpy by dheera on Saturday June 19, 2021 @07:55PM (#27565682)
It always boggles me why people went with null-terminated strings in C instead of just putting the length of the string in the first 4 bytes and then the string after that.

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.

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.

Re:Designing a better strcpy by gpderetta on Sunday June 20, 2021 @05:29AM (#27568482)
ideally the length should go with the pointer, not with the string.
Re:Designing a better strcpy by vore on Saturday June 19, 2021 @10:35PM (#27566617)
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.
Re:Designing a better strcpy by saagarjha on Saturday June 19, 2021 @08:32PM (#27565887)
Because this is how strings in C have always been done and this is how the language and pretty much all libraries treat strings.