Re: Null terminated strings: bad or good?



Harald van Dijk <truedfx@xxxxxxxxx> writes:
On Sat, 10 Jan 2009 01:46:45 -0800, just.a.garbageman wrote:
The standard allows implementations to store the length of the string
somewhere (like, say at string_ptr - sizeof (size_t)). To be more
precise, it does not disallow it. (nor it mentions it)

The standard allows this for string literals, but it would have little
benefit as functions cannot make use of it reliably. The standard
guarantees that strlen("Hello") is 5, but equally that
strlen("Hello" + 1) is 4. 4 cannot be stored in the bytes before "ello".

And consider this:

char str[] = "Hello";
str[3] = rand() % CHAR_MAX;

strlen(str) is now either 5 or 3, depending on whether the value you
stored happened to be 0.

Even more fun:

char str[] = "Hello"; /* perhaps this is at file scope */
/* ... */
char *ptr = some_arbitrary_pointer_value();
*ptr = some_arbitrary_char_value();

Did strlen(str) change? If so, to what? The compiler might be able
to prove that it didn't change in some cases, but in general you can't
tell without re-scanning the array.

Ok, suppose you recompute the stored strlen() every time the array
might have changed.

char str[] = "Hello";
str[5] = '.';

Now str doesn't even contain a string (which is perfectly legitimate
as long as you don't try to treat it as one). What value do you store
in the hidden strlen()? And how much time will your program spend
re-scanning arrays rather than doing actual work?

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Fortran/C string interop?
    ... Please note that there is nothing at all about NUL termination in the ... passing a C_PTR which points to the first character of a fixed string, ... be done with some array cases without extra help from the standard. ...
    (comp.lang.fortran)
  • Re: int array to string?
    ... Is there a standard library function I can use? ... >next integer from the array onto the growing string, ... >adequate memory set aside for this task). ...
    (comp.lang.c)
  • Re: bus errors accessing a char * by index?!
    ... The problem is that you're trying to modify a string literal. ... Any attempt to modify this array, ... You might want to look at the standard strtok() function. ...
    (comp.lang.c)
  • Re: an string array problem
    ... current standard, you may still use the "set" container. ... N items using an unsorted array). ... A hashset (stdext::hashset in recent VC ... whether the string is in the set). ...
    (microsoft.public.vc.language)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)