473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sign error in K&R 1

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 ?

Nov 14 '05 #1
2 1172
Old Wolf wrote:

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 ?


It got corrected and improved.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #2
h
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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 ?

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Dunlop | last post by:
Q: What does @ (at-sign) do? A: @ is an operator, which, when prepended to an expression, suppresses error messages. Example: ...
2
by: Greg Zwierzynski | last post by:
HI , I am passing few parameters from one page to another. One of the parameters is order number. In one particular case the order number includes "&" sign : FE:.AT&T001 Next page reads the...
1
by: johndesp | last post by:
I am building an xml document to represent a url request. Some urls contain d equal signs appended to the url. Its important I preserve the equal sign in hte xml document. However, my parser is...
1
by: Marco W | last post by:
Hi, <xsl:value-of select="translate(current(),'€','EUR')" /> only replace the euro sign with 'E' and not 'EUR'. How can I replace the euro sign in the string 'EUR'?
1
by: lbbs | last post by:
Newbie, so please don't flame me. I am just starting to learn access. I seem a feature in access a while ago, but don't remember what it was called. I seen project lab example that had, a plus...
3
by: Vadim | last post by:
I want to make strong name for may apllication assemblies, but I don't know where and how can I do it in Whidbey. Help me please
3
by: DVir | last post by:
I am having problem with IE. This is the code that I am using, and it works good in every browser except in IE. If somebody can help PLEASE: <script language="Javascript" type="text/javascript">...
0
monirul arfin
by: monirul arfin | last post by:
Hi all, when I'm using yahoo mssenger it is automatically sign out & after few second it is automatically sign in. I thought that it could be messnger problem, so I reinstall it, but same problem....
0
by: ketakijoshi | last post by:
Hi, I am pretty new to C# .Net. Working on a piece of code where I have converted Chinese characters into hex equivalents. But when I insert it in the XML, the "&" sign is converted into the "&amp;"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.