Re: Bug/Gross InEfficiency in HeathField's fgetline program



¬a\/b wrote:
In data 15 Oct 2007 11:54:09 -0700, J. J. Farrell scrisse:
On Oct 13, 6:32 pm, "¬a\\/b" <a...@xxx> wrote:
In data Sat, 13 Oct 2007 00:59:07 +0200, Tor Rustad scrisse:
[in conversation with Richard Heathfield]

[Re: implementing an equivalent of strncpy(t,s,n)]
....
What about the non-trivial case, where buffers overlap?
I'm not entirely sure this can be done in standard C, at least not the
obvious way, although I'd be glad to be proved wrong.
Hmm.. haven't written it before, my first try would be:
/* pre-condition: checking for overlapped objects */
N = strlen(s);

This function doesn't require that the source be a null terminated string. It could legally be a pointer to a character array of at least n characters, that contains no null characters. If so, the strlen() call will not terminate at the end of the array, so this code would have undefined behavior. What is really needed can be written as follows:

for(N=0; N<n && s[N]; N++);

Alternatively,

void *end = memchar(s, '\0', n);
N = (end ? (char*)end - s : n);

which might be faster, depending about how poorly my loop is optimized, and how well memchar() is optimized.

for (i=0; i<N; i++) {
for (j=0; j<N; j++)
assert(t + i != s + j);
}

and if for some i t+i==0 ?
and if for some j s+j==0 ?

What is the problem that you see in that case? Null pointer values can be compared for equality other pointer values, with well-defined behavior. The problem that you could worry about is that t+i or s+j might go more than one past the end of the array that they point into.

However, the implementor might as well assume that both t and s point into sufficiently large arrays, since as far as I know there's no way to test against the possibility that they don't.

yes this goes in segfault for strlen() in the "s" case

There's no guarantee in the C standard that it goes into segfault. That's platform-dependent.

why the string "t" has len == N?

It doesn't matter to this function whether or not t points at a string, much less what the length of that string is. All that matters is that t must point at a position where you can write at least n characters with defined behavior.
.



Relevant Pages

  • Re: Structures with variable length array known at compile time
    ... the array "menu_items" will always have 20 character strings but the ... You have the array the wrong what round -- what you wrote is an array ... of 20 arrays of q characters each. ... One way is to have a pointer to an array. ...
    (comp.lang.c)
  • Re: difference between string literal declarations
    ... an array of characters. ... The definition for 's' allocates a pointer in addition to the ... storage of the character string "hello, ... 't' only allocates the string characters. ...
    (comp.std.c)
  • Re: fgets not doing as I expect.
    ... I wanted to limit how many chars I read in, ... number of characters specified by n from the stream pointed ... immediately after the last character read into the array. ... null pointer is returned. ...
    (comp.lang.c)
  • Re: Is C++ a type-safe language ??
    ... iostream.h is not part of the C++ standard anymore. ... How do you figure that an array of chars all of a sudden denotes a pointer ... characters, has become a pointer, or a constant pointer? ...
    (comp.lang.cpp)
  • Re: Help the helpless.. code read
    ... >Barry Schwarz wrote: ... >> You wrote exactly 40 characters to the file. ... >the contents of the array remain unchanged and a null pointer ... I should have checked the standard rather than depend on the brief ...
    (comp.lang.c)