C26451:在 4 字节值上使用运算符“+”然后将结果转换为 8 字节值的算术溢出
C26451: Arithmetic overflow using operator '+' on a 4 byte value then casting the result to 8 byte value
我正在尝试编写一个程序,使用两种不同的字符串搜索算法来搜索电影剧本。然而警告 C26451: Arithmetic overflow using operator '+' on a 4 byte value then casting the result to 8 byte value keeps coming up in the calculate hash part of the rabin karp, is无论如何要解决这个问题?任何帮助将不胜感激。
#define d 256
Position rabinkarp(const string& pat, const string& text) {
int M = pat.size();
int N = text.size();
int i, j;
int p = 0; // hash value for pattern
int t = 0; // hash value for txt
int h = 1;
int q = 101;
// The value of h would be "pow(d, M-1)%q"
for (i = 0; i < M - 1; i++)
h = (h * d) % q;
// Calculate the hash value of pattern and first
// window of text
for (i = 0; i < M; i++)
{
p = (d * p + pat[i]) % q;
t = (d * t + text[i]) % q;
}
// Slide the pattern over text one by one
for (i = 0; i <= N - M; i++)
{
// Check the hash values of current window of text
// and pattern. If the hash values match then only
// check for characters on by one
if (p == t)
{
/* Check for characters one by one */
for (j = 0; j < M; j++)
{
if (text[i + j] != pat[j])
break;
}
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M)
return i;
}
// Calculate hash value for next window of text: Remove
// leading digit, add trailing digit
if (i < N - M)
{
t = (d * (t - text[i] * h) + text[i + M]) % q;// <---- warning is here
[i + M
// We might get negative value of t, converting it
// to positive
if (t < 0)
t = (t + q);
}
}
return -1;
}
context for the error
您要添加两个 int
,在您的情况下是 4 个字节,而 std::string::size_type
在您的情况下可能是 8 个字节。当您执行以下操作时会发生上述转换:
text[i + M]
这是对 std::string::operator[]
的调用,以 std::string::size_type
作为参数。
使用std::string::size_type
,通常与size_t
相同。
gcc 没有给出任何警告,即使 -Wall -Wextra -pedantic
,所以我猜你真的激活了所有你能激活的警告,或者类似的东西
我正在尝试编写一个程序,使用两种不同的字符串搜索算法来搜索电影剧本。然而警告 C26451: Arithmetic overflow using operator '+' on a 4 byte value then casting the result to 8 byte value keeps coming up in the calculate hash part of the rabin karp, is无论如何要解决这个问题?任何帮助将不胜感激。
#define d 256
Position rabinkarp(const string& pat, const string& text) {
int M = pat.size();
int N = text.size();
int i, j;
int p = 0; // hash value for pattern
int t = 0; // hash value for txt
int h = 1;
int q = 101;
// The value of h would be "pow(d, M-1)%q"
for (i = 0; i < M - 1; i++)
h = (h * d) % q;
// Calculate the hash value of pattern and first
// window of text
for (i = 0; i < M; i++)
{
p = (d * p + pat[i]) % q;
t = (d * t + text[i]) % q;
}
// Slide the pattern over text one by one
for (i = 0; i <= N - M; i++)
{
// Check the hash values of current window of text
// and pattern. If the hash values match then only
// check for characters on by one
if (p == t)
{
/* Check for characters one by one */
for (j = 0; j < M; j++)
{
if (text[i + j] != pat[j])
break;
}
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M)
return i;
}
// Calculate hash value for next window of text: Remove
// leading digit, add trailing digit
if (i < N - M)
{
t = (d * (t - text[i] * h) + text[i + M]) % q;// <---- warning is here
[i + M
// We might get negative value of t, converting it
// to positive
if (t < 0)
t = (t + q);
}
}
return -1;
}
context for the error
您要添加两个 int
,在您的情况下是 4 个字节,而 std::string::size_type
在您的情况下可能是 8 个字节。当您执行以下操作时会发生上述转换:
text[i + M]
这是对 std::string::operator[]
的调用,以 std::string::size_type
作为参数。
使用std::string::size_type
,通常与size_t
相同。
gcc 没有给出任何警告,即使 -Wall -Wextra -pedantic
,所以我猜你真的激活了所有你能激活的警告,或者类似的东西