
The Most Expensive One-Byte Mistake: Was Nul-Terminated Text Strings Wrong? - pcr910303
https://queue.acm.org/detail.cfm?id=2010365
======
perl4ever
Null terminated strings always seemed to me like a terrible idea, but on the
other hand, 255-character maximums aren't really reasonable.

What we need are strings prefixed with a length of the length of the string
and then the length as a variable length integer.

------
downerending
This would be a lot more compelling if the author also listed the benefits of
nul-terminated strings.

~~~
olliej
The only benefit is a very slight reduction in memory usage. Even that is up
for debate as the cost of computing the length of a null terminated string is
so high that variable length integer for the size is going to typically be the
same space, and even when not will still be faster.

Null termination was easy to implement quickly. It’s bad everywhere else.

~~~
downerending
Guess this would make a good interview question.

Another benefit is that you don't have to decide on a maximum string length.
Initially, they probably would have chosen a uint16 for the length, but over
time we would have had to add uint32 and uint64. Old code doesn't completely
fade away, so we would have had three similar but not-quite-compatible string
types for all time.

A small further benefit is that strings with the same suffix can share the
same storage.

I suspect there's more that I'm missing.

~~~
olliej
Variable length integers are used in many places (protobuf, utf, etc), so you
could also have unbound length there as well.

My 100% perfect concept ( :D ) would be [length]<here's the
pointer>[data]where length would be read high to low. Because of the
functionally upper capped length (maximum address space) we can treat
accessing that as O(1) vs. O(N) for null termination, etc.

~~~
7786655
You're overcomplicating the problem.

    
    
        void accept_string(const char *a, size_t a_length);
    

is good enough. It has the additional advantage that you can give it a
substring out of a larger string without copying it.

------
olliej
Can we add (2011) to the title?

