Sign error in K&R 1
From: Old Wolf (oldwolf_at_inspire.net.nz)
Date: 01/31/05
- Next message: xarax: "Re: Bit-fields and integral promotion"
- Previous message: Chris Williams: "Re: Finished a "teach yourself" book, what next?"
- Next in thread: CBFalconer: "Re: Sign error in K&R 1"
- Reply: CBFalconer: "Re: Sign error in K&R 1"
- Reply: Ishtiyaq: "Re: Sign error in K&R 1"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Jan 2005 17:23:57 -0800
I noticed the following today (I always see something new when
I look at K&R ..) , which doesn't seem to be on any errata
lists I can find.
In section 6.6 a hashing function is defined:
. hash(s)
. char *s;
. {
. int hashval;
.
. for (hashval = 0; *s != '\0'; )
. hashval += *s++;
. return(hashval % HASHSIZE)
. }
and later it is used: hashtab[hash(s)].
My observation is that s may contain chars with negative
values, making 'hashval' possibly negative. So the return
value could be negative too (p. 188 says "On all machines
covered by this manual, the remainder [of / or %] has the
same sign as the dividend").
So it causes undefined behaviour when hashtab is looked
up with a negative index.
Did this example make it into K&R2 ?
- Next message: xarax: "Re: Bit-fields and integral promotion"
- Previous message: Chris Williams: "Re: Finished a "teach yourself" book, what next?"
- Next in thread: CBFalconer: "Re: Sign error in K&R 1"
- Reply: CBFalconer: "Re: Sign error in K&R 1"
- Reply: Ishtiyaq: "Re: Sign error in K&R 1"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|