3 ms·
> he only advantage of this representation is space efficency. Not even close to true. Simplicity, future proofing are two more at least. Reference to PDP-11
by emmelaich 8y ago
> he only advantage of this representation is space efficency.
Not even close to true. Simplicity, future proofing are two more at least.
Reference to PDP-11 assembler is a furphy.
You only have two ways to store anything at the lowest level: 1. length + data or 2. data with sentinel value.
If K&R chose option 1 we would have versions of strings with 8 bitlengths, 16 bit lengths, ..... all the while incurring a base load of inefficiency within any program. (In fact I'd warrant programs would use data+sentinel internally anyway.)
There are now many many "safe string" libraries for C.
Use them if you like. The fact there are so many and get so little use tells you something.
Is length+data safer? It's easy to lie on the wire, so I don't think so. If much.
BTW, one way to provide future proof (length, data) format is to encode the length with UTF-style encoding. So the length field would have enormous range.