3 ms·
Nul termination is not only in C but also in some file formats. It has some advantages - apart from modest space savings for short strings, it means that you ca
by jstimpfle 4y ago
Nul termination is not only in C but also in some file formats. It has some advantages - apart from modest space savings for short strings, it means that you can read a string from some given location to the end - without any out of band data (length) that necessarily has to be stored in a different, agreed on location. This is a very valuable property.
I'm not saying don't use length fields, I'm saying use nul terminators where possible and use length fields where needed. And, they are not mutually exclusive.
And C doesn't need a standardized length delineated string structure in my opinion. Nul terminators serve the job fine for most Standard APIs (which take only short strings), and can receive length fields as separate function parameters where required.
- shadowofneptune 4y agoA case against: • On the most widely used architectures, reading a string is much easier if the string is a known length. x86 has its string instructions, ARM has its Load Multiple instructions. • Even with length-prefixed strings, many uses of short strings are with string literals and so the length does not need to be stored anywhere.
- jstimpfle 4y agoI agree. We're not disagreeing :)