Lists and Seg fault
Apr 9, 2014 at 11:40pm UTC
Hi, I know this is a little vague, but I keep getting a seg fault in my code every time it runs. I am trying to make a hash table that is an array of lists. right now I am focusing on the method that inserts an item into the hash table. I have tried different things, and I have no idea what's wrong. My best guess is that for some reason, the size of my list is null? Maybe? Anyway, here is my
here is the h file for hash table
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
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <list>
#include <iostream>
#include <string.h>
using namespace std;
class HashTable {
public :
HashTable();
HashTable(int size);
~HashTable();
void setHash(bool whichHash);
void insert(string name);
void delName(string name);
bool search(string name);
void logFile();
long hashFunction(string name);
unsigned long hash(string name);
unsigned long sdbm(string name);
private :
int size;
int numEntries;
int numFull;
double loadfactor;//num entries divided by num of buckets(index) i.e. 80 entries/ a table size of 10 = 8
list<string> *l;
bool whichHash;
};
#endif
Here is the HashTable.cpp file (the search and delete methods aren't shown. I am just focusing on the insert method). The program is breaking on the statement that reads "if(l[bucket].empty())"
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
#include "HashTable.h"
#include <iostream>
#include <list>
#include <sstream>
#include <string.h>
#include <fstream>
using namespace std;
HashTable::HashTable(){}
HashTable::HashTable(int size){
this ->size = size;
this ->numEntries = 0;
this ->numFull = 0;
this ->loadfactor = 0;
this ->l = new list<string>;
}
HashTable::~HashTable()
{
delete [] l;
}
void HashTable::insert(string name){
unsigned long bucket = hashFunction(name);
if (l[bucket].empty()){
l[bucket].push_back(name);
}else {
for (list<string>::iterator it=l[bucket].begin(); it!=l[bucket].end(); it++){
if (it->compare(name) == 0)
{
cout<<"name was found, cannot insert" <<endl;
cout<<"bucket: " <<bucket<<" name: " <<name<<endl;
return ;
}
}
l[bucket].push_back(name);
this ->numEntries++;
}
}
void HashTable::setHash(bool whichHash){
this ->whichHash = whichHash;
}
long HashTable::hashFunction(string name){
unsigned long hashed;
if (this ->whichHash == true ){
hashed = hash(name);
hashed = hashed%this ->size;
return hashed;
}else {
hashed = sdbm(name);
hashed = hashed%this ->size;
return hashed;
}
}
unsigned long HashTable::hash(string name){
unsigned long hash = 5381;
int c;
for (int i = 0; i < name.length(); i++)
{
c = name[i];
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
}
unsigned long HashTable::sdbm(string name){
unsigned long hash = 0;
int c;
for (int i = 0; i < name.length(); i++)
{
c = name[i];
hash = c + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
And lastly, here is my main.cpp file.
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
#include <fstream>
#include <string.h>
#include <sstream>
#include <iostream>
#include "HashTable.h"
using namespace std;
int main()
{
HashTable *list = new HashTable(88799);
cout<<"MENU" <<endl;
cout<<"(C)reate Hash Table" <<endl;
cout<<"(I))nsert new Entry" <<endl;
cout<<"(D)elete Entry" <<endl;
cout<<"(S)earch by last name" <<endl;
cout<<"(L)ogfile" <<endl;
cout<<"(Q)uit" <<endl;
string line;
bool t = true ;
while (t){
cin >> line;
if (line == "C" || line == "c" ){
ifstream myfile;
string file;
cout<<"Please enter file name: " <<endl;
cin >>file;
while (true ){
cout<<"If you wish to use has function H(s) = djb2(s)modM, press enter 'd'." <<endl;
cout<<"If you wish to use has function H(s) = sdbm(s)modM, press enter 's'." <<endl;
string which;
cin >> which;
if (which == "d" || which == "D" ){
list->setHash(true );
break ;
}else if (which == "s" || which == "S" ){
list->setHash(false );
break ;
}else {
cout<<"bad input, try again" <<endl;
}
}
myfile.open(file.c_str(), ios::in);
string name;
//load files
//system("pause");
if (myfile.good()){
cout<<"successful" <<endl;
while (!myfile.eof()){
myfile >> name;
list->insert(name);
}
// system("pause");
}else {
cout<<"file does not exist" <<endl;
}
}else if (line == "I" || line == "i" ){
string name;
cout<<"Please enter a name to be inserted:" <<endl;
cin >> name;
list->insert(name);
}else if (line == "D" || line == "d" ){
string name;
cout<<"Please enter a name to be deleted:" <<endl;
cin >> name;
list->delName(name);
}else if (line == "S" || line == "s" ){
string name;
cout<<"Please enter a name to search for:" <<endl;
cin >> name;
list->search(name);
}else if (line == "L" || line == "l" ){
list->logFile();
}else if (line == "Q" || line == "q" ){
t = false ;
list->~HashTable();
}else {
cout<<"Error: Invalid input. Try again." <<endl;
}
}
}
If someone could help me get this working, that would be awesome. THank you so much.
Apr 10, 2014 at 12:12am UTC
Nevermind, I got it to work. THank you :).
Topic archived. No new replies allowed.