Re: Null terminated strings: bad or good?
- From: "Tony" <tony@xxxxxx>
- Date: Wed, 7 Jan 2009 23:14:36 -0600
"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
.
- Follow-Ups:
- 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?
- 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
- Null terminated strings: bad or good?
- Prev by Date: Re: Null terminated strings: bad or good?
- Next by Date: Re: Null terminated strings: bad or good?
- Previous by thread: Re: Null terminated strings: bad or good?
- Next by thread: Re: Null terminated strings: bad or good?
- Index(es):
Relevant Pages
|