For starters my program is to create a hash table of names where collison is handled by chaining.
So i create an array of Linked List. Problem is my array is very large 88801 to be exact. Im thinking thats why im getting a stack over flow but im not sure. My code is below. Some one tried telling me to make the array a set of pointers to linked list.
IE putting LinkedList* hashbucket1[88801] which this removed the stack overflow issue but caused other linking errors. Guess im trying to figure out what is the best way to impliment an array of linked list of strings. If im on the right track and its something small im over looking please point it out or tell me to scrap this and start over with a given path in mind. If i need to link or zip up my other files let me know and i can.
#include <iostream>
#include "LinkedList.h"
#include <string>
#include <fstream>
using namespace std;
unsigned long djb2(string& str)
{
unsigned long hash = 5381;
for(string::iterator it = str.begin();it!=str.end();it++)
hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
return hash;
}
static unsigned long sdbm(string& str)
{
unsigned long hash = 0;
int c;
for(string::iterator it = str.begin();it!=str.end();it++)
hash = *it + (hash << 6) + (hash << 16) - hash;
return hash;
}
void insert(LinkedList HashList1[], LinkedList HashList2[], int listSize);
int main() {
char command;
int listSize = 88801;
LinkedList HashList1[88801];
LinkedList HashList2[88801];
bool invalidcommand;
do{
do{
cout << "MENU" << endl;
cout << "(I)nsert new entry" << endl;
cout << "(S)earch" << endl;
cout << "(D)elete Entry" << endl;
cout << "(C)reate Hast Table" << endl;
cout << "(L)og File" << endl;
cout << "(Q)uit and Exit Program" << endl;
cin >> command;
if(command == 'I'){
insert(HashList1, HashList2, listSize);
}else if(command == 'S'){
// search(HashList1, HashList2, listSize);
}else if(command == 'D'){
}else if(command == 'L'){
// save(HashList1, HashList2, listSize);
}else if(command == 'C'){
// load(HashList1, HashList2, listSize);
}
}while(command != 'Q');
return 0;
};
void insert(LinkedList HashList1[], LinkedList HashList2[], int listSize){
string lastName;
bool invalidID;
//Person newPerson;
do
{
invalidID = false;
cout << "Please enter Last Name: ";
cin >> lastName;
while(cin.fail())
{
cin.clear();
cin.ignore();
}
}while(invalidID);
//newPerson.setLastName(lastName);
int hashBucket1;
int hashBucket2;
hashBucket1 = djb2(lastName) % listSize;
hashBucket2 = sdbm(lastName) % listSize;
LinkedList bucket1;
if(!HashList1[hashBucket1].find(lastName)){
//HashList1[hashBucket1].insert(newPerson);
HashList1[hashBucket1].insert(lastName);
}else{
if(!bucket1.find(lastName)){
HashList1[hashBucket1].insert(lastName);
}else{
cout << "List already contains an entry with the last name " << lastName << endl;
}
}
LinkedList bucket2;
if(HashList2[hashBucket2].find(lastName)){
// bucket2.insert(newPerson);
}else{
if(!bucket2.find(lastName)){
//bucket2.insert(newPerson);
}else{
cout << "List already contains an entry with the last name " << lastName << endl;
}
}
}
Your problem, as you correctly suspect, is that you are running out of stack space. You shouldn’t be placing such big objects on the stack, which is a limited and precious resource. Instead, you can use pointers and allocate them dynamically on the heap.
The problematic code is here:
also note that
LinkedList* hashbucket1[88801]is an array of pointers, not a pointer to an array. What you want is:or, with
typedefto make it more readable:but by then it would be better to just do: