compare strings, strcmp
Dan Mick
dan at charyb.COM
Sat Nov 18 05:37:50 AEST 1989
In article <5205 at mentor.cc.purdue.edu> aic at mentor.cc.purdue.edu (George A. Basar) writes:
|
|In article <308 at charyb.COM>, dan at charyb.COM (Dan Mick) writes:
|>In article <11605 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
|>>#define StrEq( a, b ) (*(a) == *(b) && strcmp( a, b ) == 0) /* UNSAFE */
|>
|> Why the UNSAFE comment? This looks like utterly standard C to me...
|> --
|
| It is not unsafe, it is just that he was looking for a a way to not
|perform the strcmp if the first chars were unequal. He stated it was
|unsafe(performance-wise) since the order of evaluation is suspect. So the
|strcmp may be performed before the test for *(a)==*(b).
| A more 'safe' StrEq is
|
|#define StrEq(a,b) ((*(a) == *(b))?strcmp(a,b):*(a)-*(b))
|
| This will guarantee order of evaluation, but with the added overhead of
|the terniary operator. Actually, the difference is only 3 instructions in
|favor of my version(22 to 19).
|
What?
First of all, I'd think Doug knows whether or not he meant "unsafe"; after
all, the comment is pretty unambiguous. Secondly, how can there be any
other evaluation order, considering the precedence and the short-circuit
behavior of &&?
This looks suspiciously like "Here, here, I've got an answer" but it's
*wrong* and *unhelpful*.
Sigh.
--
.sig files are idiotic and wasteful.
More information about the Comp.lang.c
mailing list