Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: "James Kuyper Jr." <jameskuyper@xxxxxxxxxxx>
- Date: Wed, 17 Oct 2007 13:17:04 GMT
¬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)]
....
Hmm.. haven't written it before, my first try would be: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.
/* 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.
.
- References:
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Richard
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: kuyper
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Tor Rustad
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Richard Heathfield
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Tor Rustad
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Richard Heathfield
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Tor Rustad
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Richard Heathfield
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Tor Rustad
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: ¬a\\/b
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: J. J. Farrell
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: ¬a\\/b
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- Prev by Date: Re: How can i read the stack frames of running process?
- Next by Date: Re: how to use a variable without reference
- Previous by thread: Re: Bug/Gross InEfficiency in HeathField's fgetline program
- Next by thread: Re: Bug/Gross InEfficiency in HeathField's fgetline program
- Index(es):
Relevant Pages
|