compare strings, strcmp

George A. Basar aic at mentor.cc.purdue.edu
Fri Nov 17 02:51:03 AEST 1989


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).

* George A. Basar                              (317)742-8799 (home)
* aic at mentor.cc.purdue.edu                     BASAR at PURCCVM.BITNET    
| General Consultant                   	       (317)494-1787 (work)
| Purdue University Computing Center

Discalimer: My opinions are just that, mine.



More information about the Comp.lang.c mailing list