4 ms·
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 h
by downerending 7y ago
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 7y agoVariable 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 7y agoYou'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.