Hash Table Error Help!!

This is the error that I'm getting:

Unhandled exception at 0x013c4218 in Hash_Table.exe: 0xC0000005: Access violation reading location 0x0000001c.


Here's where my debugger is telling me where the error is:
1
2
3
4
5
// This is in the xstring file
int compare(const _Myt& _Right) const
{	// compare [0, _Mysize) with _Right
	return (compare(0, this->_Mysize, _Right._Myptr(), _Right.size()));
}


I don't know anything about xstring. I'm still learning about data structures here.

This is what my code looks like.
Driver[UPDATED]:
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
#include <cctype>		// Provides toupper
#include <cstdlib>		// Provides EXIT_SUCCESS
#include <cstring>		// Provides strchr
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>		// Operate on strings
#include "hTable.h"
using namespace std;

// get total weight
// Precon: pass in preconstructed table, a string to parse and find weight, and the start of the string
// Postcon: calculates one line from the file and does the appropriate math for molecular weights
double getTWeight(hTable& hashTable, string line, int i);

// reads the table
// Precon: pass in a preconstructed hash table by reference and a string 
// Postcon: reads in one line of code and parses it and puts it in the table
void readHTable(hTable& hashTable, string line);

int main()
{
	string line;
	hTable hashTable;
	string source = "PeriodicTableElements.txt";
	ifstream file_input;
	file_input.open(source);
	if(file_input.fail())
	{
		cerr<<"Could not open input file."<<endl;
		system("PAUSE");
		exit(0);
	}
	while(file_input.peek()!=EOF)
	{
		getline(file_input, line, '\n');
		readHTable(hashTable, line);
	}
	file_input.close();		// End of insertion of elements into the hash table
	
	//**********************************************************************//

	string source1 = "formulas.txt";
	ifstream file_input1;
	file_input1.open(source1);
	if(file_input1.fail())
	{
		cerr<<"Could not open formula file."<<endl;
		system("PAUSE");
		exit(0);
	}
	while(file_input1.peek()!=EOF)
	{
		getline(file_input1, line, '\n');
		double tWeight=getTWeight(hashTable, line, 0);
		cout<<line<<"="<<tWeight<<endl;
	}
	file_input1.close();
	cout<<endl;
	system("PAUSE");
	return EXIT_SUCCESS;
}

void readHTable(hTable& hashTable, string line)
{
	double weight;
	int i=0;
	while(line[i] != ' ')
		++i;
	string element=line.substr(0, i);
	int elementNumber = element[0]-0;
	int weightLength=line.size()-i;
	string weightString=line.substr(i,weightLength);
	istringstream convert(weightString);
	if( !(convert >> weight) )
		weight=0;
	hashTable.insert(element, weight, elementNumber);
}

double getTWeight(hTable& hashTable, string line, int i)
{
	int j;
	int multiply;
	double tWeight=0.0;
	double weight;
	while(line[i] != '\0')
	{
		j=i;
		if(line[i]=='(')
		{
			++i;
			int k = i;
			while(line[k+1] != ')')
				k++;
			string lineHelp=line.substr(i,(k-i+1));
			weight=getTWeight(hashTable, lineHelp, 0);
			i=(k+2);
			if(line[i] == '\0')
				tWeight = (tWeight + (weight*1));
		}
		else
		{
			while(islower(line[i+1]))
				i++;
			int k=(i-(j+1));
			string element = line.substr(j, k);
			double elementNumber = element[0]-0;
			weight = hashTable.retrieve(elementNumber, element);
			++i;
			if( !(isdigit(line[i])) )
				tWeight = (tWeight + (weight*1));
		}
		j=i;
		while(isdigit(line[i]))
			i++;
		int k = (i-j);
		string inputPasser = line.substr(j, k);
		istringstream convert(inputPasser);
		if( !(convert>>multiply))
			multiply = 0;
		tWeight = (tWeight + (weight*multiply));
	}
	return tWeight;
}


hTable.cpp
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
#include "hTable.h"
#include<iostream>
#include<string>

hTable::hTable()
{
	for(int i=0;i<SIZE;i++)
		Array[i]=new node(-1, " ");
}

hTable::~hTable()
{
	for(int i=0; i<SIZE; i++)
	{
		if(Array[i]->next != nullptr)
			Delete(Array[i]->next);
		delete Array[i];
	}
}

void hTable::Delete(node* n)
{
	if(n->next!=nullptr)
		Delete(n->next);
	delete n;
	return;
}

void hTable::insert(string pElement, double pWeight, double key)
{
	int index=hash(key);
	if(Array[index]->data==-1 || Array[index]->data==-2)
	{
		Array[index]->data=pWeight;
		Array[index]->element=pElement;
	}
	else
	{
		insert(Array[index]->next, pElement, pWeight);
	}
}

void hTable::insert(node*& n, string pElement, double pWeight)
{
	if(n==nullptr)
		n=new node(pWeight, pElement);
	else
		insert(n->next, pElement, pWeight);
}

double hTable::retrieve(double d, string element)
{
	int index = hash(d);
	if(Array[index]->element == element)
		return Array[index]->data;
	else
		return retrieve(d, element, Array[index]->next);
}

double hTable::retrieve(double d, string element, node* n)
{
	if(n->element==element)
		return n->data;
	return retrieve(d, element, n->next);
}

int hTable::hash(double d)
{
	int a=((int)(d/.05555)%SIZE);
	return a;
}


and my hTable.h
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
#pragma once
#include<cstdlib>
#include<string>

using namespace std;
struct node
{
	double data;
	string element;
	node* next;
	node(double d, string ele)
	{
		data=d;
		element=ele;
		next=nullptr;
	}
};

const int SIZE = 50;
class hTable
{
	node* Array[SIZE];
public:
	// constructor
	hTable();
	// destructor
	~hTable();
	// helps to delete dynamic allocated memory
	void Delete(node* n);
	// inserts elemental data from the P-Table into a hash table
	void insert(string, double, double);
	// inserts elemental data from the P-Table into a hash table when there is a collision
	void insert(node*& n, string element, double weight);
	// returns the value for the element
	double retrieve(double d, string element);
	// returns value for element when there is a collision
	double retrieve(double d, string element, node* n);
	// hash function to spread out the entries from the inserted data
	int hash(double);
	// overload the [] brackets
	hTable operator [] (int);
	// Clear (remove all from hash table)
	void clear(node* Array[]){hTable::~hTable();}
};


Can someone help me out with this error? Thanks in advance.
Last edited on
When you hit the error in your debugger, look up the call stack and see where in your code the error is occurring.
My debugger points to this:
 
return (compare(0, this->_Mysize, _Right._Myptr(), _Right.size()));


This is also where the call stack points to.

> Hash_Table.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::compare(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Right) Line 1853 + 0x15 bytes C++
I am assuming you did not write any of the code in xstring, and so there is an error in your code that is calling code there improperly. I want you to use your debugger to look at the call stack and see where in your code you are when the error is hit.
Access violation reading location 0x0000001c.


That location looks like a nullptr representation + a small offset. My guess would be that you are dereferencing a null pointer.
Ok so I stepped through with my debugger and the error comes into play when the compiler tries to execute Line 55 on my driver.

1
2
3
4
5
6
while(file_input1.peek()!=EOF)
	{
		getline(file_input1, line, '\n');
	        double tWeight=getTWeight(hashTable, line, 0);      // This line
		cout<<line<<"="<<tWeight<<endl;
	}


UPDATE:
There error actually comes into play on line 62 in my hTable.cpp of my overloaded retrieve()
 
	if(n->element == element)
Last edited on
I'd check, as cire suggested, that hashTable isn't nullptr.
So n->element has these members set
data : Error: expression cannot be evaluated
element : {npos=4294967295}

and ele has this set
ele : H2SO4

I'm not seeing where hashTable is/isn't nullptr. This is my first hash table so I could be overlooking something somewhere.
Last edited on
UPDATE:
There error actually comes into play on line 62 in my hTable.cpp of my overloaded retrieve()

 
	if(n->element == element)


And where in that do you ensure you aren't dereferencing a null pointer? Can n be null?
I'm looking at my hash function and wondering if that could possibly be the problem. Otherwise, I'm at a complete loss here.

UPDATE:
I still got nothing here but I'll keep burning the midnight oil on this one. It's got to be there somewhere.
Last edited on
I'm looking at my hash function and wondering if that could possibly be the problem. Otherwise, I'm at a complete loss here.


The error message suggests that you dereference a null pointer. Your debugging indicates the error occurs in retrieve on a specific line which dereferences a pointer via the -> operator. Did you see the questions I asked?

I don't know how much clearer the problem can be.
Yes I saw your questions and I get what the problem is but I'm at a complete loss on how to fix it. I was just trying out different approaches based upon my level of understanding with this. I still feel somewhat of an amateur.
just make sure n is valid.
if(n)
{
code here
}

that will make sure that n is valid and not a null value.


this is my guess and I'm not 100% sure if this is the correct answer
Last edited on
natekelsey wrote:
Yes I saw your questions and I get what the problem is but I'm at a complete loss on how to fix it. I was just trying out different approaches based upon my level of understanding with this. I still feel somewhat of an amateur.

Once you've pinpointed where the problem is occurring, you have to ask yourself: What sequence of events leads to this happening at this particular place? You might want make adjustments to your code (or set breakpoints in the debugger) at points where this function is called so that it becomes more obvious to you when and why the issue occurs. Troubleshooting your code is a huge part of the programming process. You should thank yourself for giving you practice. ;)
Topic archived. No new replies allowed.