Hash Table Segmentation Fault

Hello, I am working on a Hash Table program. It compiles, however it gets a segmentation fault. Any idea why? Thanks.

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
 #include <iostream>
#include <cstddef>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

template <typename KEY, typename VALUE>
class Entry
{
	template<typename K, typename V> friend class HashTable;

private:
	KEY key;
	VALUE value;
	Entry * next_entry;

public:
	/*** Constructors ***/
	Entry(void) : key(KEY()), value(VALUE()), next_entry(NULL) {}
	//If you would like you can convert constructors to have this look
	~Entry();
	Entry(KEY key, VALUE value)
	{
		this->template key = key;
		this->template value = value;
		this->template next_entry = NULL;
	}
	Entry(KEY key, VALUE value, Entry * next) : key(key), value(value), next_entry(next) {}

	//accessors
	KEY get_key() { return key; }
	VALUE get_value() { return value; }
	Entry * get_next_entry() { return next_entry; }

	//mutators
	void set_key(KEY key) { this->key = key; }
	void set_value(VALUE value) { this->value = value; }
	void set_next_entry(Entry * entry) { this->next_entry = entry; }
	// is this the thing that should change?
};


template<typename K, typename V>
class HashTable
{
private:
	int N;
	int count;
public:
	
	Entry<K, V> **hash_table;
	HashTable()
	{
		this->N = 10000;
		hash_table = new Entry<K, V> *[this->N];

		for (int i = 0; i < this->N; i++)
			hash_table[i] = NULL;
	}

	HashTable(int N)
	{
		this->N = N;
		hash_table = new Entry<K, V> *[this->N];

		for (int i = 0; i < this->N; i++)
		{
			hash_table[i] = NULL;
		}
	}
	~HashTable()
	{
		
		delete [] hash_table;
		
	}
	
	


	void resize(bool grow)
	{

		int buckets = this->N;
		Entry<K, V> ** old = this->hash_table;
		Entry<K, V> * swap;
	

		hash_table = new Entry<K, V> *[count];
		for (int i = 0; i < count; i++)
		{
			hash_table[i] = NULL;
		}


		for (int i = 0; i < buckets; i++)
		{
			while (old[i] != NULL)
			{
				swap = old[i];
				old[i] = swap->get_next_entry();
				insert(swap);
			}
		}
		delete[] old;

	}


	int hashcode(string s)
	{
		int hash_value = 0;
		for (int i = 0; i < s.length(); i++)
		{
			hash_value = (127 * hash_value + (unsigned char)s[i]) % 16908799;
		}
		return hash_value;
	}

	int hashcode(int i)
	{
		int hash_value = (127 * i) % 16908799;
		return hash_value;
	}


	int hashcode(char c)
	{
		int hash_value = static_cast<int>(c);
		hash_value = (127 * hash_value) % 16908799;
		return hash_value;
	}


	int hashcode(unsigned long ul)
	{
		int hash_code;

		if (ul <= 2147483647)
		{
			hash_code = static_cast<int>(ul);
		}
		else
		{
			ul = ul / 2;
			hash_code = static_cast<int>(ul);
			hashcode = 0 - hash_code;
		}

		return hash_code;

	}


	int compression(int hc)
	{
		int compress = ((5 * hc + 6) % this->N);
		return compress;
	}


	Entry<K, V> * find(Entry<K, V> * e)
	{
		int hc;
		V value = e->get_value();
		hc = hashcode(value);
		hc = compression(hc);
		Entry<K, V> * box = hash_table[hc];
		return box;

	}

	void replace(Entry<K, V> * e1, Entry<K, V> * e2)
	{
		e2->set_key(e1->get_key());
		e2->set_value(e1->get_value());
		delete e1;
	}

	void remove(Entry<K, V> * e)
	{
		int hc;
		V value = e->get_value();
		hc = hashcode(value);
		hc = compression(hc);
		Entry<K, V> * track = hash_table[hc];
		Entry<K, V> * temp = hash_table[hc];
		while (e != track)
		{
			if (track != NULL)
			{
				temp = track;
				track = track->get_next_entry();
			}
			else
			{
				return; // this doesnt tell the user there wasnt anything deleted, but it shouldnt need to.
			}
		}

		temp->set_next(track->get_next_entry());
		delete track;
		count--;

	}
	void insert(Entry<K, V> * e)
	{
		int hc;
		V value = e->get_value();
		hc = hashcode(value);
		hc = compression(hc);


		if (hash_table[hc] == NULL)
		{
			hash_table[hc] = e;
		}

		else
		{
			Entry<K, V> * swap;
			swap = hash_table[hc];
			e->set_next_entry(swap);
			hash_table[hc] = e;
		}

		count++;

	}

	/* if grow = 1 increase the table
	* otherwise decrease the table
	*/






	double compute_load_factor()
	{
		int load = this->count / this->N;
		// 0.8 - 1.2     n/N  where n = number of entries and N = number of buckets
		return load;
	}

	int longest_chain_length()
	{
		int count = 0;
		int highest = 0;
		Entry<K, V> * track;
		for (int i = 0; i < this->N; i++)
		{
			track = this->hash_table[i];
			if (track != NULL)
			{
				count++;
			}
			while (track->get_next_entry() != NULL)
			{
				count++;
				track = track->get_next_entry();
			}
			if (count > highest)
			{
				highest = count;
			}
			count = 0;

		}
		return highest;
	}
};



int main()
{
	//seed the random number generator
	srand(42);

	HashTable<unsigned long, string>  table_01(10); //Should be 10k
	unsigned long key;
	string value;
	Entry<unsigned long, string> * e;

	// Fill the table with random entries
	for (int i = 0; i < 20; i++)
	{
		/* create a random entry */
		key = (sizeof(int) < sizeof(long)) ? (static_cast<int>(((unsigned long)rand()) << (sizeof(int) * 8)) | rand()) : rand(); // what
		value = "";
		for (int j = 0; j < (rand() % 45 + 1); j++)
		{
			value += 'a' + rand() % 26;
			e = new Entry<unsigned long, string>(key, value);

		}
		table_01.insert(e);
	}
	cout << "Longest Chain: " << table_01.longest_chain_length() << endl;
	cout << "Load Factor: " << table_01.compute_load_factor() << endl;

	table_01.resize(true);
	cout << "Longest Chain: " << table_01.longest_chain_length() << endl;
	cout << "Load Factor: " << table_01.compute_load_factor() << endl;

	return 0;
}
Nevermind I fixed it. Problem was with my table initialization in main being different than my table entries. Solved.
Topic archived. No new replies allowed.