0xC0000005:访问冲突读取位置0x00000000哈希函数
0xC0000005: Access violation reading location 0x00000000 hashfunc
我试图执行我的程序并收到此错误消息在这里我的主要
int numofsect=2;
unsigned char** hash_table;
hash_table = new unsigned char*[numofsect];
for (int i=0; i < numofsect; i++)
hash_table[i] = new unsigned char[CryptoPP::SHA::DIGESTSIZE];
char** tab;
tab = new char*[numofsect];
for (int i=0; i< numofsect; i++)
tab[i] = new char[5];
int* tabsize;
tabsize = new int[2];
tabsize[0]=5;
tabsize[1]=5;
printf("Type sections:n");
printf("Sect1: ");
scanf("%s", tab[0]);
printf("nSect2: ");
scanf("%s", tab[1]);
hasher(numofsect, tab, tabsize, hash_table);
printf("Your hashed tab is:n");
printf("hash sect1: ");
printf("%s",hash_table[0]);
printf("nhash sect2: ");
printf("%s",hash_table[1]);
delete[] hash_table;
delete[] tab;
delete[] tabsize;
这是我的哈希函数:
void hasher (int num_of_sect, char** sect_tab, int* size_of_sect_tab, unsigned char** hash_tab )
{
for (int i=0 ; i<=num_of_sect ; i++) { // i is the number of each secion
byte haSha1[CryptoPP::SHA::DIGESTSIZE]; //Byte table to calculate the hash of the section i
byte* chaine = (byte*)malloc(sizeof(byte)*size_of_sect_tab[i]); //chaine reiceive the byte stream of the section i
for (int j=0 ; j<size_of_sect_tab[i]; j++) //j is the n-th byte of the section
chaine[j]=sect_tab[i][j]; //copy each byte of the sect_tab in the chaine
CryptoPP::SHA().CalculateDigest(haSha1, chaine, size_of_sect_tab[i]); //Hash the section and return it in haSha1
for (int j=0; j<CryptoPP::SHA::DIGESTSIZE; j++) //j is the n-th byte of the hashed section
hash_tab[i][j]=haSha1[j]; //copy each byte of the hash to the hash_table
}
}
感谢您的帮助
当 i = 在以下代码中num_of_sect时,您正在访问 sect_tab 数组的边界之外:
chaine[j]=sect_tab[i][j];
你不能有
for (int i=0 ; i<=num_of_sect ; i++) {
由于您传递制表符 = 新字符*[数字部分];
请记住,数组的索引范围从 0 到 size-1。
哈希
器函数中的 for 循环不应该<=
,而应该像这样<
:
for (int i=0 ; i < num_of_sect ; i++)
这可能就是问题所在。只需使用调试器并逐步完成它,您很快就会看到问题所在。调试器是我们最好的朋友!
从零开始的索引!
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 使用Qt C++计算类似Git的SHA1哈希
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- c++找不到具有相同哈希的无序集合元素
- 哈希文件递归并保存到矢量Cryptopp中
- 对 pair<pair<int,int>pair<int,int unordered_map进行哈希处理>>
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 如何为字符串生成唯一但一致的 N 位哈希(小于 64 位)?
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- boost::包含提升单元的元组的哈希值
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- C++中的并发哈希表
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 如何为位集找到/实现一个好的哈希函数
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 0xC0000005:访问冲突读取位置0x00000000哈希函数