Re: Null terminated strings: bad or good?
- From: Richard <rgrdev_@xxxxxxxxx>
- Date: Thu, 08 Jan 2009 13:24:10 +0100
"Tony" <tony@xxxxxx> writes:
"Richard" <rgrdev_@xxxxxxxxx> wrote in message
news:gk3f1j$1d6$7@xxxxxxxxxxxxxxxxxxxxxxxx
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Tony wrote:
"Dik T. Winter" <Dik.Winter@xxxxxx> wrote:
"Tony" <tony@xxxxxx> writes:
...
Well said (meaning, I read that, understand it). I still don't see
the benefit of null terminated strings over a struct-like thing:
struct string {
uint32 length;
char* data;
};
And the advantage of that?
1. Ease of IO (writing to/fro disk files, for example).
2. No need to calculate length (a real bummer with null terminated
strings).
None of that appears in C. The writing from a string can be done
by:
int wrtstring(char *s, FILE *f) {
char ch;
int err;
while (ch = *s++) {
if (EOF == (err = putc(ch, f))) break;
}
return err;
}
Note that putc can be a macro, and thus can use the file system
buffers directly. This can make the overall writing very
efficient. However it must not have side effects on the
arguments. Thus the ch is required. If no error occurs wrtstring
returns the last char written.
Standard beginner error #1 : You forgot the input condition where *s ==
0. Result is UB and dirty underpants flying out your nostrils or
whatever it is.
We won't bother being so pedantic as mentioning if S is NULL to start
with.
Surely he was just showing the main gist of the null-terminated string
writing and didn't intend to suggest that the given function was completed
production code.
Tony
One can only judge by the code given. Normally I would not have
mentioned it, but CBF is a pretty nasty piece of work who won't "help"
anyone who does not post full and complete code. One can only assume he
meant the code as the one true way. But regardless, that entire function
was nonsense because of the UB. Now, I don't really think that, but
that's the way we are trained by the c.l.c regs to think.
--
I'm not a person who particularly had heros when growing up.
- Dennis Ritchie when asked about the hero worship coming from c.l.c
.
- References:
- Null terminated strings: bad or good?
- From: Tony
- Re: Null terminated strings: bad or good?
- From: blargg
- Re: Null terminated strings: bad or good?
- From: Tony
- Re: Null terminated strings: bad or good?
- From: Dik T. Winter
- Re: Null terminated strings: bad or good?
- From: Tony
- Re: Null terminated strings: bad or good?
- From: CBFalconer
- Re: Null terminated strings: bad or good?
- From: Richard
- Re: Null terminated strings: bad or good?
- From: Tony
- Null terminated strings: bad or good?
- Prev by Date: Re: how to avoid mistaken integer comparisons
- Next by Date: Re: Writing a function to figure out if stack grows up or down
- Previous by thread: Re: Null terminated strings: bad or good?
- Next by thread: Re: Null terminated strings: bad or good?
- Index(es):
Relevant Pages
|