Path: utzoo!yunexus!lethe!torsqnt!jarvis.csri.toronto.edu!mailrus!purdue!mentor.cc.purdue.edu!aic From: aic@mentor.cc.purdue.edu (George A. Basar) Newsgroups: comp.lang.c Subject: Re: compare strings, strcmp Summary: !unsafe, just maybe as slow as strcmp alone Keywords: strcmp,strings Message-ID: <5205@mentor.cc.purdue.edu> Date: 16 Nov 89 15:51:03 GMT Article-I.D.: mentor.5205 References: <4463@blake.acs.washington.edu> <11605@smoke.BRL.MIL> <308@charyb.COM> Organization: Buckaroo Banzai Institute Lines: 26 In article <308@charyb.COM>, dan@charyb.COM (Dan Mick) writes: >In article <11605@smoke.BRL.MIL> gwyn@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@mentor.cc.purdue.edu BASAR@PURCCVM.BITNET | General Consultant (317)494-1787 (work) | Purdue University Computing Center Discalimer: My opinions are just that, mine.