我收到此错误"Unhandled exception at 0x009437bd in Hash functions.exe: 0xC0000005: Access violation reading
I get this error "Unhandled exception at 0x009437bd in Hash functions.exe: 0xC0000005: Access violation reading location 0x00000001"
当我运行我的项目时,我一次又一次地得到这个错误。不知道问题出在哪里。我知道这是关于我再次使用的一些空闲内存,或者一个空指针或访问一个不存在的内存,但实际上我检查了所有的指针,它们被声明为它们应该是什么。
代码如下:
#include <iostream>
using namespace std ;
typedef struct ND {
int ID;
char* Name;
char* Address;
int Age;
double GPA;
ND * next;
} NODE;
class HF {
private :
int count;
int size ;
int prime;
int a ,b;
NODE ** HT;
public:
HF ();
HF ( int n , int p , int a, int b);
~ HF ();
int findindex(int key);
bool insert (int ID,char* Name,char* Address,int Age,double GPA);
bool retrieve (int & ID,char* & Name,char* & Address,int & Age,double & GPA);
bool remove(int key);
double GetLoadFactor ();
};
HF :: HF ()
{
size = 100;
prime = 997;
a = 23 ;
b = 88;
count =0;
HT = new NODE* [size];
for (int i=0; i< size ; i++)
HT[i] = NULL;
}
HF :: HF ( int n , int p , int a, int b)
{
size = n;
prime = p;
a = a;
b = b;
count = 0;
HT = new NODE* [size];
for (int i=0; i< size ; i++)
HT[i] = NULL;
}
HF :: ~ HF ()
{
NODE *p;
for (int i=0 ; i<size ; i++)
{
while (HT[i] != NULL)
{
p = HT[i];
HT[i] = HT[i] -> next ;
delete p;
}
}
delete [] HT ;
}
int HF :: findindex(int key)
{
int index ;
index = (((a*(key)+b) % prime) % size) ;
index = index % size ;
return index;
}
bool HF :: insert (int ID,char* Name,char* Address,int Age,double GPA)
{
int i ;
NODE * n;
n = new NODE;
n -> ID = ID;
n -> Address = Address;
n -> Name = Name;
n -> Age = Age;
n -> GPA = GPA;
n -> next = NULL;
i = findindex(ID);
if ( HT[i] == NULL)
{
HT[i] = n;
}
else
{
n -> next = HT[i];
HT[i] = n;
}
count ++ ;
return true;
}
bool HF ::retrieve (int & key,char* & Name,char * &Address,int & Age,double & GPA)
{
int i ;
NODE *p;
i = findindex(key);
if ( HT[i] == NULL)
{
return false;
}
else
{
p = HT[i];
if ( HT[i] -> ID == key)// here is the break point
{
key = p-> ID ;
Name = p-> Name ;
Address = p-> Address ;
Age = p-> Age;
GPA = p-> GPA ;
return true ;
}
while ( p != NULL)
{
if ( p-> ID == key)
{
key = p-> ID ;
Name = p-> Name ;
Address = p-> Address ;
Age = p-> Age;
GPA = p-> GPA ;
return true ;
}
else
p = p-> next ;
}
}
return false;
}
bool HF :: remove (int key)
{
int i ;
NODE *p1 , *p2;
i = findindex(key);
if ( HT[i] == NULL)
{
return false;
}
else
{
p1 =p2 = HT[i];
if(HT[i] -> ID == key)
{
HT[i] = HT[i] -> next;
delete p2;
return true;
}
while ( p2 != NULL)
{
if ( p2 -> ID == key)
{
p1 -> next = p2 -> next ;
delete p2;
count --;
return true;
}
else
{
p1 =p2;
p2 = p2 -> next;
}
}
}
return false;
}
double HF :: GetLoadFactor()
{
double L;
L = (double) count/size ;
return L;
}
int main ()
{
double L;
int x,age;
char * name , *address;
double GPA;
HF UHashFunc1;
HF UHashFunc2( 11 , 7 , 3 , 0);
UHashFunc1.insert( 6 , "Ahmed" , "Jenin" , 20 , 3.5);
UHashFunc1.insert( 1 , "Sarah" , "Jenin" , 18 , 3.2);
UHashFunc1.insert(40 , "Mohammad" , "Tolkrem", 19 , 3.0);
UHashFunc1.insert(2 , "Ala'a" , "Jerusalem", 19 , 2.6);
UHashFunc1.insert(41 , "Raghad" , "Tolkrem", 19 , 1.6);
UHashFunc1.insert(80 , "Mohammad" , "Jenin", 22 , 2.7);
UHashFunc1.insert(83 , "Murad" , "Nablus", 18 , 3.7);
UHashFunc1.insert(44 , "Reem" , "Hebron", 19 , 2.9);
UHashFunc1.insert(50 , "Wajde" , "Qalqelya", 20, 1.7);
UHashFunc1.insert(42 , "Belal" , "Hebron", 20 , 3.4);
UHashFunc1.insert(3 , "Ahmed" , "Nablus", 21 , 1.9);
UHashFunc1.insert(84 , "Haitham" , "Nablus", 21 , 3.1);
cout <<"enter the ID you want to retrieve"<<endl;
cin>>x;
if(UHashFunc1.retrieve(x,name,address,age,GPA))
{
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
}
else
cout<<"NOT FOUND"<<endl;
cout <<"enter the ID you want to retrieve"<<endl;
cin>>x;
if(UHashFunc1.retrieve(x,name,address,age,GPA))
{
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
}
else
cout<<"NOT FOUND"<<endl;
L=UHashFunc1.GetLoadFactor();
cout << "The current load factor is : " << L <<endl;
UHashFunc1.remove(42);
L=UHashFunc1.GetLoadFactor();
cout << "The current load factor is : " << L <<endl;
x=84;
UHashFunc1.retrieve(x,name,address,age,GPA);
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
x=1;
UHashFunc1.retrieve(x,name,address,age,GPA);
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
x=50;
UHashFunc1.retrieve(x,name,address,age,GPA);
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
cout << "Enter The ID you want to remove"<<endl;
cin>>x;
if(UHashFunc1.remove(x))
{
L=UHashFunc1.GetLoadFactor();
cout << "The current load factor after removing a record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
if(UHashFunc1.remove(2))
{
L=UHashFunc1.GetLoadFactor();
cout << "The current load factor after removing a record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
UHashFunc1.insert( 45 , "Amjad" , "Nablus" , 19 , 2.0);
L=UHashFunc1.GetLoadFactor();
cout << "The current load factor after adding a record is : " << L <<endl;
if(UHashFunc1.remove(80))
{
L=UHashFunc1.GetLoadFactor();
cout << "The current load factor after removing the record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
if(UHashFunc1.remove(50))
{
L=UHashFunc1.GetLoadFactor();
cout << "The current load factor after removing the record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
UHashFunc2.insert( 5 , "Ahmed" , "Jenin" , 20 , 3.5);
UHashFunc2.insert( 1 , "Sarah" , "Jenin" , 18 , 3.2);
UHashFunc2.insert(9 , "Mohammad" , "Tolkrem", 19 , 3.0);
UHashFunc2.insert(2 , "Ala'a" , "Jerusalem", 19 , 2.6);
UHashFunc2.insert(8 , "Raghad" , "Tolkrem", 19 , 1.6);
UHashFunc2.insert(100 , "Mohammad" , "Jenin", 22 , 2.7);
UHashFunc2.insert(50 , "Murad" , "Nablus", 18 , 3.7);
UHashFunc2.insert(23 , "Reem" , "Hebron", 19 , 2.9);
UHashFunc2.insert(40 , "Wajde" , "Qalqelya", 20, 1.7);
UHashFunc2.insert(17 , "Belal" , "Hebron", 20 , 3.4);
UHashFunc2.insert(3 , "Ahmed" , "Nablus", 21 , 1.9);
UHashFunc2.insert(7 , "Haitham" , "Nablus", 21 , 3.1);
cout <<"enter the ID you want to retrieve from the 2nd Func"<<endl;
cin>>x;
if(UHashFunc2.retrieve(x,name,address,age,GPA))
{
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
}
else
cout<<"NOT FOUND"<<endl;
cout <<"enter the ID you want to retrieve from the 2nd Func"<<endl;
cin>>x;
if(UHashFunc2.retrieve(x,name,address,age,GPA))
{
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
}
else
cout<<"NOT FOUND"<<endl;
L=UHashFunc2.GetLoadFactor();
cout << "The current load factor is : " << L <<endl;
UHashFunc2.remove(2);
L=UHashFunc2.GetLoadFactor();
cout << "The current load factor is : " << L <<endl;
x=5;
UHashFunc2.retrieve(x,name,address,age,GPA);
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
x=1;
UHashFunc2.retrieve(x,name,address,age,GPA);
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
x=50;
UHashFunc2.retrieve(x,name,address,age,GPA);
cout << "ID:"<<x<<endl;
cout << "Name:"<<name<<endl;
cout << "Address:"<<address<<endl;
cout << "Age:"<<age<<endl;
cout << "GPA:"<<GPA<<endl;
cout << "Enter The ID you want to remove from the 2nd Func"<<endl;
cin>>x;
if(UHashFunc2.remove(x))
{
L=UHashFunc2.GetLoadFactor();
cout << "The current load factor after removing a record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
if(UHashFunc2.remove(2))
{
L=UHashFunc2.GetLoadFactor();
cout << "The current load factor after removing a record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
UHashFunc2.insert( 45 , "Amjad" , "Nablus" , 19 , 2.0);
L=UHashFunc2.GetLoadFactor();
cout << "The current load factor after adding a record is : " << L <<endl;
if(UHashFunc2.remove(100))
{
L=UHashFunc2.GetLoadFactor();
cout << "The current load factor after removing the record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
if(UHashFunc2.remove(9))
{
L=UHashFunc2.GetLoadFactor();
cout << "The current load factor after removing the record is : " << L <<endl;
}
else
{ cout << "NOT Exist"<<endl;}
cin>>x;
return 0;
}
没有构建错误只有当断点
时在Hash functions.exe中0x009437bd的第一次机会异常:0xC0000005:访问冲突读取位置0x00000001。未处理的在Hash functions.exe中的0x009437bd异常:0xC0000005: Access违规读取位置0x00000001。第一次例外0x009437bd在Hash functions.exe: 0xC0000005:访问违规读取0 x00000001位置。线程"Win32线程"(0x1e40)已退出代码-1073741510 (0xc000013a)。程序的[788]哈希functions.exe: Native' has exit,代码为-1073741510 (0xc000013a).
我可以很容易地重现您遇到的错误。HF有一个构造函数,其参数如下:
HF :: HF ( int n , int p , int a, int b)
{
size = n;
prime = p;
a = a;
b = b;
可以看到,参数名a和b与成员变量名完全相同。因此HF::a和HF::b永远不会赋值。因此,它们总是未初始化并包含垃圾。因此,你得到的只是随机值,而你试图做的所有哈希操作都不起作用。
您应该将参数名称更改为不同的名称。或许用大写字母说:
HF :: HF ( int n , int p , int A, int B)
{
size = n;
prime = p;
a = A;
b = B;
一旦我这样做,它解决了所有的内存损坏错误,并运行到完成正常
先学会使用,然后应用,std::vector<T>
, std::string
, std::shared_ptr<T>
, std::unique_ptr<T>
。
这肯定是一个NULL指针。您正在访问内存位置1 (0x00000001),它与NULL(也称为0x00000000)有1个字节的偏移。您发布的代码是冗长的,有点混乱,并且没有访问指针的行号,很难说您究竟在哪里访问NULL指针。
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 什么是"#include <boost/functional/hash.hpp> "?
- 终端不会为C++文件创建.exe文件吗
- 在Windows中以.exe的形式运行c++
- Visual Studio mkl_link_tool.exe链接错误
- PrintWindow函数在notepad.exe中出错
- VSCode C++ 编译的exe感染了Win32:TrojanX-gen[Trj]
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 获取当前正在运行的 exe 名称(不是路径)
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 1d.exe找不到SFML文件?
- VI工作室代码错误无法打开输出文件主.exe
- 如何为每个客户生成自定义 exe
- 使用参数在 C# 的C++上运行.exe
- 在 ******.exe 中0x00000000772CA267 (ntdll.dll) 处未处理的异常:0xC0000
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 解析引用 |exe/dll vs lib
- Q没有管理权限的 exe 无法启动维护工具
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- 我收到此错误"Unhandled exception at 0x009437bd in Hash functions.exe: 0xC0000005: Access violation reading