3 ms·
NUL-terminated strings are a poor representation, feature-poor and directly responsible for a great many entirely avoidable bugs. I much prefer the approach us
by chrismorgan 7y ago
NUL-terminated strings are a poor representation, feature-poor and directly responsible for a great many entirely avoidable bugs.
I much prefer the approach used by languages like Rust, where &str and &[T] are (pointer, length) rather than just a pointer.
Usage can be identical to your classic NUL-terminated string, depending on the language. (It is in Rust, with CStr.)
There is only one potential disadvantage: pointers use twice as much memory; but in practice, this disadvantage is entirely theoretical.
But there are several serious advantages: it lets you represent zero values (U+0000 for &str, integer zero for &[u8], &c.), take slices that aren’t suffixes (reducing either code complexity or memory usage, sometimes drastically), calculate the string length in constant time (and that’s regularly a big deal), and avoid a pitfall that has been the cause of many critical security vulnerabilities (buffer overrun).
Want to break a string into tokens on delimiters? Easy, regardless of the length of the delimiter (it could be zero, two, seventeen—while with NUL-terminated strings it had to be one), without needing to worry about whether it’s safe to mutate the string.
- clarry 7y ago> You speak as though programmers scampering off for library functions to do everything is a bad thing. I consider it a wonderful thing. No, that's not what they are saying. They're just saying that to blame the representation for user mistakes and then present an alternative representation where user mistakes are prevented by library functions is comparing apples to oranges.
- chrismorgan 7y agoAh yes, I did misread it there; thanks for the correction. Line deleted, the rest stands without it.
- kazinator 7y agoPointer and length representations are available in C, and we can find programs that get them wrong somewhere. "Such and such a data structure ... in the middle of Rust" is not a discussion about string representation per se any more. Rust must have null terminated strings at some level, because it has to integrate with platforms where null terminated strings are the API currency for text. Yet, Rust programs don't fall down because of null-terminated strings. Likewise, if you have a pointer and length, but no language or library support for it, just a "high level assembly language", you will screw it up. The pointer has to point to something. There will be cases where that something is a fixed buffer on the stack or in a dynamic object. Out of a hundred programmers coding their pointer and length with fixed buffers, a good many will cause overflows, and a good many will write memory-safe code that causes consequences due to silent truncation. In C, we can cause problems like: * returning a pointer to a stack-allocated { length, ptr } structure, which turns to garbage. * leak dynamically-allocated { length, ptr } objects, or use them after freeing. * making copies of a { length, ptr } structure (e.g. pass by value), and not keeping all of them in sync when ptr and/or length are updated. * sticking a literal "string" into ptr, but then treating it as dynamic elsewhere and trying to free it * sticking a literal "string" into ptr, and then somewhere trying to modify a character.