56表达式:(无符号)(c + 1)<= 256 [英] 56 Expression: (unsigned)(c + 1) <= 256
问题描述
我正在做一个哈希程序,它计算一个单词在文本文件中的每个实例的数量。这是我的计数函数:当尝试运行它时,我得到一个错误。
I am making a hashing program that is counting the number of each instance of a word in a text file. This is my count function: I am getting an error when trying to run it.
56表达式:(unsigned)(c + 1) < = 256
56 Expression: (unsigned)(c + 1) <= 256
看起来当isalpha函数读取第一个nonalpha垃圾字符文本文件。
It appears as though it is crashing on the isalpha function when it is reading in the very first nonalpha garbage characters in the text file.
int
count(ifstream & fs,int size)
{
int find(const char *,int, int);
int f,i,l,y;
char ch,*p,s[maxs+1];
for(y = l = i = 0; i < size; i++)
{
table[i].k = 0;
table[i].p = nill;
}
p = s;
while(fs.get(ch))
{
if(isalpha(ch))
{
if(l < maxs)
{
l++;
*p++ = (char)(ch | 0x20);
}
}
else
{
if(l)
{
*p = '\0';
if(f = find(s,size,l) < 0)
{
return(f);
}
y += f;
p = s;
l = 0;
}
}
}
}
推荐答案
在我看来, isalpha
是失败的断言。很可能(unsigned)(c + 1)<= 256
是被断言的表达式。看起来这个断言试图确保 c
的值落在[0,255]内。
It looks to me like isalpha
is failing an assertion. Most likely (unsigned)(c + 1) <= 256
is the expression that is being asserted. It looks like this assertion is trying to ensure the value of c
falls within [0, 255].
假设 ch
是一个 signed char
,您尝试将值128存储在其中,然后传递给 isalpha
,断言的左侧将计算一个非常大的数字,导致它失败。
Assuming ch
is a signed char
and you try to store the value 128 in it, then pass it to isalpha
, the left hand side of the assertion is going to evaluate to a very large number, causing it to fail.
128不能存储在 signed char
中,因此 ch
实际上变为-128,它是无符号128(二进制为1000 0000)的有符号表示。 isalpha
将 ch
作为 int
code>(c + 1)实际上是( - 128 + 1)
,它变为-127。
128 can't be stored in a signed char
, so the value of ch
actually becomes -128, which is the signed representation of unsigned 128 (1000 0000 in binary). isalpha
is taking ch
as an int
, so the (c + 1)
is actually (-128 + 1)
, which becomes -127. This value is then cast to an unsigned integer, which turns into a very large value.
一个解决方案是更改 ch
在代码中添加到 unsigned char
,如果它的值可能大于127.
A solution is to change ch
in your code to an unsigned char
, if it's possible that it's value can be greater than 127.
这篇关于56表达式:(无符号)(c + 1)<= 256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!