c++ - 编译哈希算法时出现内存访问冲突错误
问题描述
将字符串插入内存时出现错误。0xC0000005 访问冲突错误。我试图更改导致分配错误的数组大小。int 主要部分仅将字符串单词发送到插入函数,直到文件结束。
#include<string>
#include<iostream>
#include <fstream>
using namespace std;
const unsigned int MAX = INT16_MAX;
string *words = new string[MAX];
int* instances = new int[MAX];
//int* alloc = new int[MAX];
int ucounts = 0;
static unsigned int FNVHash(string str) {
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
unsigned int i = 0;
unsigned int len = str.length();
for (i = 0; i < len; i++)
{
hash *= fnv_prime;
hash ^= (str[i]);
}
return hash;
}
void insert(string input) {
//check first, add if not present
if (words[FNVHash(input)] != input) { //<-Compiler shows error here.
words[FNVHash(input)] = input;
instances[FNVHash(input)]=1;
ucounts++;
}
else {
instances[FNVHash(input)]++;
}
}
解决方案
您没有任何东西可以将返回的值限制FNVHash
在words
. 要么FNVHash
需要确保散列在 [0..NAX] 范围内,要么用户 ( insert
) 需要这样做。
推荐阅读
- visual-studio - Onedrive 不会进入文件资源管理器
- android - 无法识别语句'import com.google.android.maps.MapActivity; '
- gitlab - Gitlab.com (saas) CI 运行器公共或私有 IP 地址(范围)
- javascript - Firebase 数据库用户身份验证:使用电话提供商的 UID 创建用户电子邮件和密码
- r - 来自应用函数迭代列名的ggplot2轴标签
- javascript - JavaScript - 对象函数
- python - Python - 如果变量在打印函数中,则换行,需要修复
- java - 通过 MakeSignature.signDeferred 进行外部签名时,Java iText 5 签名无效
- python - 有没有办法在 Geopy 中调试 Nominatim 的 SSL 错误和最大重试错误?
- linux - 如何在 Linux 上模拟中断风暴或直播?