News for hackers. Stuff that matters.
Re:Sentinels Can Be Faster by throwaway894345 on Wednesday September 09, 2020 @03:57PM (#24425231)
This seems useful if you're expecting to pass your strings to a lot of C libraries, which is probably sensible for C++ (especially whatever vintage of C++ happened to introduce this string design); however, surely this is a negligible concern for most mainstream languages?
Re:Sentinels Can Be Faster by pengaru on Wednesday September 09, 2020 @04:03PM (#24425305)
> This seems useful if you're expecting to pass your strings to a lot of C libraries, which is probably sensible for C++ (especially whatever vintage of C++ happened to introduce this string design); however, surely this is a negligible concern for most mainstream languages?

No, the system call interface of the operating system expects NUL-terminated strings as parameters all over the place, especially the filesystem APIs.

Re:Sentinels Can Be Faster by steveklabnik on Wednesday September 09, 2020 @04:23PM (#24425516)
Who came first, the chicken or the egg?

EDIT: Okay, this is at -1, maybe I shouldn't be slightly obtuse. These questions are, in my mind, two sides of the same coin. The OS expects null terminated strings, not for some inherent reason, but because C and UNIX grew up together. There's no inherent reason you can't have an OS that does not use sentinels to demarcate strings.

Re:Sentinels Can Be Faster by throwaway894345 on Wednesday September 09, 2020 @05:50PM (#24426339)
Probably my fault for phrasing the question ambiguously, but the parent interpreted my question as I intended it to be interpreted. Interfacing with the system libraries is a common enough case for using null-terminated strings.

I'm actually a little curious how what these low-level APIs look like and how "modern" languages handle efficiently converting to null-terminated strings. Do they just copy everything while C/C++/etc would pass a straight reference? Also, what do the underlying syscall APIs look like?

Re:Sentinels Can Be Faster by steveklabnik on Wednesday September 09, 2020 @05:55PM (#24426378)
It's all good. You are 100% right that it's still a good use-case.

And yeah, in Rust we have a separate CString type that's null terminated, so you may need a copy to move between the two worlds. It just depends, if you're only reading, for example, then you can create a pointer + len where the length doesn't include the 0, and that's effectively zero cost.

You got me curious about my days as a kid putting "pascal" in front of Mac System 7 stuff; I believe that it only accepted Pascal strings. I also vaguely remember people doing funny stuff like null terminating pascal strings anyway and then putting the length before the actual pointer so that you get both in one...

Re:Sentinels Can Be Faster by colejohnson66 on Wednesday September 09, 2020 @06:12PM (#24426520)
Weren’t “Pascal strings” limited in that they were only a byte though? So, your string could only be 255 bytes long (conveniently going to a nice round 256 when you add the length byte).
Re:Sentinels Can Be Faster by steveklabnik on Wednesday September 09, 2020 @06:15PM (#24426538)
Yep!
Re:Sentinels Can Be Faster by colejohnson66 on Wednesday September 09, 2020 @04:04PM (#24425312)
For most people, the time difference between reading 4/8 bytes and scanning for a null terminator is negligible, but there’s the other benefit of not having to worry as much about buffer overruns; If you append to a std::string and its internal buffer is full, it’ll realloc() its buffer. This does have the problem of the possibility of a realloc(), but I prefer that over buffer overruns.