Re: Null terminated strings: bad or good?



"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
.



Relevant Pages

  • [PATCH 09/21] perf: rewire generic library stuff, p5
    ... +int eprintf(int level, const char *fmt, ...) ... * Helper function for splitting a string into an argv-like array. ... +static int count_argc(const char *str) ...
    (Linux-Kernel)
  • Re: Is char obsolete?
    ... I'm talking specifically about the type "char". ... intended to be interpreted as a character in the basic execution ... part of the encoding of a string. ... Perhaps one where we just have a "struct string" ...
    (comp.lang.c)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: Returning a character buffer from a DLL
    ... I need to return a string buffer from the DLL in a RunQuery function. ... I find it odd that you are using the obsolete 'char *' data type here. ... want to use a string pointer of any type here! ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)