Problem running hashtable
Hi, am creating this hashtable, but am getting a core dumped and I think it's because am not creating my table correctly. Can somebody please point me to the right direction. Here is my code
Code:
#include <iostream>
#include <string>
#include <fstream>
//#include <stdio.h>
//#include <ctype.h>
const int h_size = 10000000;
struct town
{
public:
//They are all string types because all the info being collected is just for display purpose.
string name;
string population;
string housing;
string lan_area;
string water_area;
string square_land;
string square_water;
string x_cord;
string y_cord;
town(string a, string b, string c,string d, string e, string f, string g, string h, string i)
{
name = a;
population = b;
housing = c;
lan_area = d;
water_area = e;
square_land = f;
square_water = g;
x_cord = h;
y_cord = i;
}
};
string erasestring(string x, int size)
{
int count = 0;
string temp;
for(int i = 0; i < size-1; i++)
{
if(x[i] == ' ')
count++;
}
x.erase(0,count);
temp = x;
return temp;;
}
class hashtable
{
public:
int data_item;
int hash_size;
class hashlink
{
public:
town * data;
hashlink * next;
hashlink(town * t, hashlink * n = NULL);
};
hashlink * * table;
int hash(string s);
public:
hashtable( int init_size = h_size);
void add(town * t);
};
hashtable::hashtable(int init_size)
{
hash_size = init_size;
table = new hashlink * [init_size];
for (int i = 0; i < init_size; i++)
table[i] = NULL;
}
void readfile()
{
hashtable * table;
string mainline;
string line1;
string line2;
string line3;
string line4;
string line5;
string line6;
string line7;
string line8;
string line9;
int count = 0;
string temp;
ifstream getf("alphaplaces.txt");
//while(getf.fail() != true)
// {
getline(getf,mainline);
line1 = mainline.substr(0,38);
line2 = erasestring(mainline.substr(70,13),13);
line3 = erasestring(mainline.substr(82,9),9);
line4 = erasestring(mainline.substr(91,14),14);
line5 = erasestring(mainline.substr(105,14),14);
line6 = erasestring(mainline.substr(119,12),12);
line7 = erasestring(mainline.substr(131,12),12);
line8 = erasestring(mainline.substr(144,9),9);
line9 = erasestring(mainline.substr(153,11),11);
//cout << "\n" << pos << "\n";
town * x = new town(mainline.substr(0,38),line2,line3,line4,line5,line6,line7,line8,line9);
table->add(x);
// }
}
void hashtable::add(town * t)
{
int pos = hash(t->name);
cout << endl;
hashlink * p = new hashlink(t,NULL);
cout << "hash# " << pos << endl;
cout <<"hash size " << hashtable::hash_size << endl;
if(hashtable::table[pos] == NULL)
{
hashtable::table[pos] = p;
cout << "testing";
}
//cout << table[pos]->name << endl;
//cout << table[pos]->population << endl;
}
int hashtable::hash(string s)
{
unsigned int h = 982322;
for (int i=0; i<s.length(); i+=1)
h = h * 97 + s[i];
return h % h_size;
}
hashtable::hashlink::hashlink(town * t,hashtable::hashlink * n)
{
data = t;
next = n;
}
void main()
{
//hashtable * maintable = NULL;
readfile();
return(1);
}
Re: Problem running hashtable
The first thing any of us would do is run it in the debugger and see what's going on. If you haven't already, I'd suggest starting there.
Re: Problem running hashtable
You will need to supply the 'alphaplaces.txt' file in case someone wants to try to reproduce your error.
Re: Problem running hashtable
Quote:
Originally Posted by
GCDEF
The first thing any of us would do is run it in the debugger...
Technically, I think I'd start by just using std::unordered_map, or boost::hashmap.
@vladic256: Can't you just use one of the above libraries? I dare say that hash tables are some of the most complex containers, and writing one should not be attempted by anything less than an actual expert, who's job is to write containers.
Do you even really need a hashmap? I have no context, so I wouldn't know your needs, but maybe a less efficient but easier to use container might be in order?
Anyways, to answer your question, just skimming through the code: inside readfile: You declare the the pointer table, and start using it before allocating anything to it.
Also, what exactly is "erasestring" supposed to do? Strip whitespaces? That's not what it's doing...
Re: Problem running hashtable
Rather than manually managing a dynamic array of linked lists, perhaps you should consider simply using a std::vector<std::list<town>>.
Re: Problem running hashtable
Quote:
Originally Posted by
monarch_dodra
Technically, I think I'd start by just using std::unordered_map, or boost::hashmap.
I was referring to finding the cause of a crash in general
Re: Problem running hashtable
Well I used the debugger, but I suspect it's the way am trying to put my info into table[pos]
Re: Problem running hashtable
Quote:
Originally Posted by
vladic256
Well I used the debugger, but I suspect it's the way am trying to put my info into table[pos]
It should have given you a pretty good idea what's wrong. As Peter said, not much we can do without your input file.
Re: Problem running hashtable
How about you allocate memory for 'table' in readfile()?
Why do you even make this a pointer?
Code:
hashtable * table;
table->add(x);
Re: Problem running hashtable
Quote:
Originally Posted by
vladic256
Well I used the debugger, but I suspect it's the way am trying to put my info into table[pos]
Quote:
Originally Posted by
monarch_dodra
Anyways, to answer your question, just skimming through the code: inside readfile: You declare the the pointer table, and start using it before allocating anything to it.
Maybe you should allocate for table