Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!charyb!dan From: dan@charyb.COM (Dan Mick) Newsgroups: comp.lang.c Subject: Re: compare strings, strcmp Keywords: strcmp,strings Message-ID: <311@charyb.COM> Date: 17 Nov 89 18:37:50 GMT References: <4463@blake.acs.washington.edu> <11605@smoke.BRL.MIL> <308@charyb.COM> <5205@mentor.cc.purdue.edu> Reply-To: dan@charyb.UUCP (Dan Mick) Organization: KFW Corporation, Newbury Park, CA Lines: 35 In article <5205@mentor.cc.purdue.edu> aic@mentor.cc.purdue.edu (George A. Basar) writes: | |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). | 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.