Re: Mystery: static variables & performance
From: Peter Nilsson (airia_at_acay.com.au)
Date: 02/21/04
- Next message: CBFalconer: "Re: K&R 2 exercise 2-3"
- Previous message: Mark McIntyre: "Re: Keyboard = Stop"
- In reply to: nrk: "Re: Mystery: static variables & performance"
- Next in thread: CBFalconer: "Re: Mystery: static variables & performance"
- Reply: CBFalconer: "Re: Mystery: static variables & performance"
- Reply: nrk: "Re: Mystery: static variables & performance"
- Reply: pete: "Re: Mystery: static variables & performance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: CBFalconer: "Re: K&R 2 exercise 2-3"
- Previous message: Mark McIntyre: "Re: Keyboard = Stop"
- In reply to: nrk: "Re: Mystery: static variables & performance"
- Next in thread: CBFalconer: "Re: Mystery: static variables & performance"
- Reply: CBFalconer: "Re: Mystery: static variables & performance"
- Reply: nrk: "Re: Mystery: static variables & performance"
- Reply: pete: "Re: Mystery: static variables & performance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|