4 ms·
Interesting read. However, the article doesn't fully consider the costs of the alternative decision. One of the things I really hated about COM programming was
by iqster 15y ago
Interesting read. However, the article doesn't fully consider the costs of the alternative decision. One of the things I really hated about COM programming was BSTRs (prefix the length of the string in memory BEFORE the actual pointer). Granted, BSTRs are a kludge but to me, it illustrated the elegance of the NULL terminated C-string vs. alternatives.
- joezydeco 15y agoThe other thing that came to mind to me was that you have now defined a maximum string length, unless you then had a meta-length number (describing the "length" of the length). And how long should that number be? I could see where Ken & Co just looked up and figured null-termination was a more elegent answer.
- tlrobinson 15y agoYou could use a variable-width size field similar to how UTF-8 works, but that's getting pretty complicated and inefficient. e.x. 0xxx xxxx 10xx xxxx xxxx xxxx 110x xxxx xxxx xxxx xxxx xxxx 1110 xxxx xxxx xxxx xxxx xxxx xxxx xxxx etc
- boredguy8 15y agoAnd re-introduces the 'magic character' you're trying to eliminate.
- deleted 15y ago[deleted]
- fuzzix 15y agoThe magic character is in a known location, there's no need to iteratively scan for it. The use of "magic" characters is not the problem, as I see it.
- boredguy8 15y agoNo it's not. In the first line, it's in [0], in the second line, it's in [1], in the third line, it's in [2]. You have to scan through to find where the '0' appears
- tlrobinson 15y agoTrue, but it's O(log N) rather than O(N) to determine length. BTW I'm not sure if it was clear or not but those are bits in my diagram, not bytes/characters. The "x"s are the bits of the length field, not the characters of the string.
- daemin 15y agoBut you could just as easily set aside the first 4 bytes of a string to be the length and then it would be O(1). But does that really matter when you're doing operations on the string that iterate over the whole string anyway? Since iterating the whole string is O(n) anyway, you're not really gaining anything.
- fuzzix 15y agoOperations to return string length or return characters from the end of the string become far less expensive. You also have a fair idea of a reasonable sized buffer to move in duplication operations, avoiding single character move loops.
- gojomo 15y agoIf we could send one message back in time to Ken, Dennis, and Brian, I nominate this.
- agazso 15y agoInterestingly it was also Ken Thomson who invented UTF-8 as well.
- aaronblohowiak 15y ago> The other thing that came to mind to me was that you have now defined a maximum string length, unless you then had a meta-length number (describing the "length" of the length). And how long should that number be? size_t
- ArbitraryLimits 15y agoBSTR's are not only null-terminated, they're double null-terminated (two bytes of zeroes).
- mellery451 15y ago..but that's just a convenience to allow them to play (mostly) nice with wchar APIs. The BSTR itself could have avoided the trailing NULLs if it never had to worry about being treated as a simple wchar buffer. http://blogs.msdn.com/b/ericlippert/archive/2003/09/12/52976.aspx http://blogs.msdn.com/b/ericlippert/archive/2003/09/12/52976...
- ArbitraryLimits 15y agoMy point was that it's not fair to point to BSTR's as an example of how the len+data approach is bad because BSTR's force you to do both and (in my opinion) end up having the drawbacks of both approaches and the benefits of neither.