3 ms·
There's another reason for using NUL-terminated strings: It means that a suffix of a string is also a string. Want to convert "hello world" into "hello" and "w
by cperciva 15y ago
There's another reason for using NUL-terminated strings: It means that a suffix of a string is also a string. Want to convert "hello world" into "hello" and "world"? Just replace the ' ' with a '\0'. With length-prefix strings, you would need to copy the "world" into a new memory allocation -- which is far too wasteful for 1970s programming.
- matthavener 15y agoOf course, assuming that unwanted space is there. With addr and len, you could split "helloworld" into "hello" and "world" by simply pointing to the two separate parts, each with len == 5. (This is actually really nice when parsing large strings into smaller strings because you can simply point all the smaller strings into a single, large string).
- colomon 15y agoThat assumes that the string characters are not adjacent to the string length field in memory... (Edited to add) More explicitly: Grandparent specified length-prefix strings, where the string's length is stored in memory immediately before the string's characters. And his claim is correct in that case. You seem to be assuming a string object which consists of a length and a pointer to the characters. What you suggest certainly would work there, but any straight-forward implementation of this is going to require another pointer's worth of memory and two memory allocations for each string, too.
- ZoFreX 15y agoThis is actually what Java does when you call substr, effectively (String objects contain a reference to a char array, a length, and an offset).
- daemin 15y agoThat's also because (to my knowledge) that strings in Java are immutable. Same as in Ruby and a bunch of other languages. So the optimisation that can be done is to put all of the string data into one big (immutable) array and have each string object just reference into it for its data.
- colomon 15y agoAnd think of the classic code for copying a NUL-terminated string. Unless I'm missing some clever optimization, replacing it with a length-based version would require another counter variable -- meaning the algorithm would use another register (or even possibly two on an 8-bit machine) and require another operation for each character. Not such a big deal now, but it would have been a big advantage for NUL-termination back in when C was designed.
- Peaker 15y agoYou can make your string pointer a "fat pointer" (basically a (ptr,size) tuple), instead of the char ptr you have now. Then substrings are still strings, you just have to make a new fat pointer to it rather than an ordinary pointer.
- delinka 15y agoSounds a bit like array slicing. ;-)
- kabdib 15y agoThe 68K Macintosh used Pascal-style strings; Str255, with a count on the front. I don't recall it ever noticably improving code quality. And it made it really, really hard to deal with long strings; everyone invented their own formats and interoperability between libraries was a nightmare.