One more point regarding = and == (more flamage)
Stephen Carlson
scc at rlgvax.Reston.ICL.COM
Thu Mar 28 03:39:49 AEST 1991
In article <7318:Mar2622:58:0391 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
>In article <1991Mar26.180311.29125 at rlgvax.Reston.ICL.COM> scc at rlgvax.OPCR.ICL.COM (Stephen Carlson) writes:
>> while (*s++ == *t++)
>> ;
>> is not well set up to compare two arrays.
>
>Actually, sentinels are among the fastest ways to do a linear search
>without wasting noticeable extra space.
If you had quoted the entire context, you would have read:
| while (*s++ == *t++)
| ;
|
| is not well set up to compare two arrays. It does not check for a zero
| termination. It is really only useful when the programmer has already
| set up guaranteed unequal sentinals at the end of the array. Then the
| pointers after the loop are one greater than would be useful.
My point was that that particular encoding is not well set up even for
sentinel searches. The post-loop pointers need to be readjusted. This
code, for example, does not suffer that disadvantage:
while (*s == *t)
s++, t++;
After the loop, the pointers are pointing at the unequal elements, not one
past the unequal elements--which may even end up past the array bounds!
Your general point about sentinels is correct, of course.
--
Stephen Carlson | ICL OFFICEPOWER Center | In theory, theory and
scc at rlgvax.reston.icl.com | 11490 Commerce Park Drive | practice are the same.
..!uunet!rlgvax!scc | Reston, VA 22091 |
More information about the Comp.lang.c
mailing list