CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    26

    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 &#37; h_size; 
    }
    
    hashtable::hashlink::hashlink(town * t,hashtable::hashlink * n)
    {
    
             data = t;
    
             next = n;
    
    }
    
    
    void main()
    {
    //hashtable * maintable = NULL;
    
    readfile();
     return(1);
    
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: Problem running hashtable

    You will need to supply the 'alphaplaces.txt' file in case someone wants to try to reproduce your error.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem running hashtable

    Quote Originally Posted by GCDEF View Post
    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...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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>>.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Problem running hashtable

    Quote Originally Posted by monarch_dodra View Post
    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

  7. #7
    Join Date
    Mar 2009
    Posts
    26

    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]

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Problem running hashtable

    Quote Originally Posted by vladic256 View Post
    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.

  9. #9
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    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);

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem running hashtable

    Quote Originally Posted by vladic256 View Post
    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 View Post
    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
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured