Jon Stewart on 27 Aug 2001 06:29:51 -0000 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: hosers-talk: Duh. |
> Quoth "Jon Stewart": > > Didja' ever notice that "" and "\0" are the same strings in C/C++? > > > > > > > > Jon > > Since all strings are null-terminated, "" should be 0x00. But what if what > you want is a string that consists of a null byte? Why isn't that stored as > 0x0000? Exactly some of the thinking that prompted my observation. In C, of course, if only by convention, a double null string is functionally equivalent to a null string. It just wastes a byte. Since wasting single bytes is abhorrent to C programmers, I doubt you'll find much usage of that. In C++, you may want an array -- not a string, although it's merely conceptual semantics differing the two -- of 0x0000. I think the best way to go about this is to abandon strings and use other constructs. I have a ByteStream class for just such purposes. My String class inherits from my ByteStream class so I can work with strings in a semi-nice way (it's still dog food), perform some hackery, and treat them like ByteStreams, for high-performance file I/O. (I used protected inheritance, so hackery is necessary, which is what I intended; treating a String as a ByteStream _should_ be kind of ugly and very much intentional.) My String class, in order to work with C libs and hardcoded strings, uses the ByteStream mechanism (which knows nothing of null-termination -- a null character is just another character) and then null-terminates strings. Consequently, most of the code in the String class proper is mediating code with the ByteStream to figure out the null-termination. What I think is funny about my observation is that it's a very good example of C's "worse is better" philosophy. I mean, compared to something like Perl, or Visual Basic, C seems morally and philosophically tight, coherent, and beautiful. But by making the concession to users that hardcoded strings, ie. "whatever", are null-terminated automatically, you end up with some curiosities. So it's not as free from special cases as we might like. Jon -- Jon Stewart stew1@xxxxxxxxxxx "Survey says: it was a bad idea in the first place." -- The Dismemberment Plan