Re: Mystery: static variables & performance

From: Peter Nilsson (airia_at_acay.com.au)
Date: 02/21/04


Date: Sat, 21 Feb 2004 12:05:38 +1100


"nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
news:yVgZb.52767$1S1.8086@nwrddc01.gnilink.net...
> Richard Heathfield wrote:
>
> > R. Rajesh Jeba Anbiah wrote:
> >
> >> nrk <ram_nrk2000@devnull.verizon.net> wrote in message
> >> news:<WiNYb.43798$1S1.37569@nwrddc01.gnilink.net>...
> >>>
> > <snip>
> >
> >>> PS: Your book, section 4.2: The WAR style example of strcmp is
> >>> atrocious
> >>> IMO. If you must insist on a single return, here's a clearer version
of
> >>> strcmp:
> >>>
> >>> int strcmp(const char *s1, const char *s2) {
> >>> while ( *s1 == *s2 && *s1 )
> >>> ++s1, ++s2;
> >>>
> >>> return *s1 - *s2;
> >>> }
> >>
> >> Thanks a lot for your interest in the quality of the book. As you
> >> see, it has it's bug reporting corner; please don't take c.l.c as the
> >> one. Thanks for your help; thanks for your understanding.
> >
> > Since he /did/ post his "clearer version" to comp.lang.c, you should at
> > least get some feedback as to what is wrong with his correction. Do you
> > see the flaw? If not, then how do can you do quality control on the bug
> > reports you receive?
> >
> > comp.lang.c is good at this sort of thing.
> >
> > (Hint: the problem I can see has nothing to do with the loop.)
> >
>
> Ok, here's my take on this:
>
> a) sizeof(int) > 1 for hosted implementations.
> http://www.google.com/groups?selm=bu63eq%24mtp%249%40sunnews.cern.ch

There mere fact that Dan Pop says so does not make it so! ;)

There are actual members of the C Committee who disagree on this.

> So, integer overflow not an issue, yes?

No. Even if sizeof(int) == 2, you can still have INT_MAX < UCHAR_MAX. [It's
the limits which are important, not the byte size.]

>
> b) Peter's concern still remains. So, does changing the last line to:
>
> return *(unsigned char *)s1 - *(unsigned char *)s2;
>
> make it alright?

No. Reading chars via an unsigned char lvalue can produce a different value
to the original.

Since character constants and I/O are based on unsigned char -> int -> char
_conversions_ when storing plain char strings, the correct answer (assuming
no integer overflow) is to use a _conversion_ of the plain char value to
unsigned char...

  return (unsigned char) *s1 - (unsigned char) *s2;

Note that on most implementations (8-bit, 2c, no padding) there is no need
to go to this extreme, although the result is the same.

The most robust answer would seem to be...

  return (unsigned char) *s1 > (unsigned char) *s2
         - (unsigned char) *s1 < (unsigned char) *s2;

or...

  return (unsigned char) *s1 < (unsigned char) *s2 ? -1
         : (unsigned char) *s1 > (unsigned char) *s2;

--
Peter


Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... When you post abusive material *and* a claim of libel in a single ... It's because of your char. ... It wasn't a correction. ... I have neither the desire nor the power to damage anyone's reputation ...
    (comp.lang.c)
  • Re: Displaying Euro Symbol
    ... Thanks a lot for your correction. ... I think I've mistaken the "€" char ... for a multi-bytes one. ... Your point on the ISO-8859-x highlight this. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: continuous data from C++ to MATLAB
    ... Ashish I made your correction to ... This time it plots two points. ... Assert: Forced Assertion: Corrupted memory block found. ... memcpy((char *) mxGetPr, data_c, ...
    (comp.soft-sys.matlab)
  • Re: print error
    ... > CHAR: 1 ... > Is the correction on microsoft website ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: ---What this mean
    ... <snip standard quote> ... >of unsigned char, which rules out negative values for space characters. ... type plain char_ yields a negative value. ... me to write a standard conforming C implementation for a platform ...
    (comp.lang.c)