Hi,
This is my first post on this forum! :)
I have been trying to implement a HashMap in C++, but I keep getting the "Process returned -1073741819 (0xC0000005)" error.
I don't get that error every single execution, but I get it frequently - that is the program sometimes executes successfully, and the rest of the time I get the error as stated above.
My code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include<iostream>
#include<cstdlib>
using namespace std;
class Link{
public:
int iData;
Link *next;
public:
Link(int d): iData(d)
{ }
int getKey(){return iData;}
void displayLink(){
cout<<iData<<" ";
}
};
class HashList{
public:
Link *first;
public:
HashList(): first(NULL)
{ }
void insertItem(Link data){
int key = data.getKey();
Link *previous = NULL;
Link *current = first;
while(current!=NULL && key > current->getKey()){
previous = current;
current = current->next;
}
Link *t = (Link*)malloc(sizeof(Link));
t->iData = data.iData;
if(previous==NULL){
first = t;
}
else{
previous->next = t;
}
t->next = current;
}
void deleteItem(int key){
Link *previous = NULL;
Link *current = first;
while(current!=NULL && key != current->getKey()){
previous = current;
current = current->next;
}
if(previous == NULL){
first = first->next;
}
else{
previous->next = current->next;
}
}
void findItem(int key){
Link *current = first;
while(current!=NULL && current->getKey()<=key){
if(current->getKey()==key){
cout<<"Item found!"<<endl;
return;
}
current = current->next;
}
cout<<"Item not found!"<<endl;
}
void displayList(){
cout<<"List: first->last"<<endl;
Link *current = first;
while(current!=NULL){
current->displayLink();
current = current->next;
}
cout<<""<<endl;
}
};
class HashTable{
public:
int size;
HashList *hLink = (HashList*)malloc(size*sizeof(HashList));
public:
HashTable(int arrsize){
size = arrsize;
int i;
for(i = 0;i<size;i++){
hLink[i] = HashList();
}
}
void displayTable(){
int i;
for(i = 0;i<size;i++){
cout<<i<<". ";
hLink[i].displayList();
}
}
int hashFunction(int key){return key%size;}
void insertData(Link d){
int key = d.getKey();
int hashVal = hashFunction(key);
cout<<"hash value for item to be added is: "<<hashVal<<endl;
hLink[hashVal].insertItem(d);
}
void deleteData(int key){
int hashVal = hashFunction(key);
cout<<"hash value for item to be deleted is: "<<hashVal<<endl;
hLink[hashVal].deleteItem(key);
}
void find(int key){
int hashVal = hashFunction(key);
cout<<"hash value for item to be searched is: "<<hashVal<<endl;
hLink[hashVal].findItem(key);
}
};
int main(){
Link a(12);
Link b(32);
Link c(82);
Link d(55);
Link e(89);
HashTable h(10);
h.insertData(a);
h.insertData(b);
h.insertData(c);
h.deleteData(12);
h.displayTable();
h.find(11);
h.find(32);
}
|
My guess is that the error is from one of the pointers, so I tried messing with the pointers and still I get the same error.
Also, can someone please explain why the program executes and gives the desired output
sometimes but is still able to give an error when executed some other time?..
EDIT:
I may have found the error.The error seems to come from the memory allocation for the HashList.
|
HashList *hLink = (HashList*)malloc(size*sizeof(HashList));
|
If I try to use something like
and just call the class as HashTable foo, it works well - no errors.
Is there any proper way that I can dynamically allocate memory for the hashtable?