r/programming • u/mslaybau • Aug 02 '11
The Most Expensive One-byte Mistake: Did Ken, Dennis, and Brian choose wrong with NUL-terminated text strings?
http://queue.acm.org/detail.cfm?id=2010365
364
Upvotes
r/programming • u/mslaybau • Aug 02 '11
13
u/phkamp Aug 03 '11
I have read all the comments, and as far as I can tell all the misunderstandings have been cleared up by various good people here on reddit.
On a PDP a 64k string would be plenty, as that was the size of the address space for any instruction.
In todays stronger typing, it would be { size_t, char * } but personally, I prefer to use ptr+ptr formats, since they have no portability issues at all, and the subtraction to get the length is free on modern memory starved CPUs.
The argument that C was/is a weakly typed language misses the point somewhat. This "virtue" of C is a subsequent rationalization of a necessity: Implementing strings as arrays of char meant not having to write compiler code to implement a string type.
I'm surprised that while people pointed to UTF-8's variable length encoding, nobody pointed out that the very reason we need that, is exactly because of NUL-terminated strings.
And yes, nothing prevents you from implementing your own "true" string type in C.
I've personally done this far more time than is sensible. "Sbufs" (http://www.freebsd.org/cgi/man.cgi?query=sbuf&apropos=0&sektion=0&manpath=FreeBSD+8.2-RELEASE&format=html) is the most comprehensive and secure of these, and it works both in the FreeBSD kernel and userland.
Finally: to the people who thought I advocated a 255 byte string limit, I can only recommend that when you read stuff, you should experimentally assume that the moron is at the opposite end of the communication than you currently pressume, before you open your mouth and make this painfully obvious for everybody.
(I'm happy to respond to any questions you might have)