Error when trying to implement a hash table class

This is a discussion on Error when trying to implement a hash table class within the C++ Programming forums, part of the General Programming Boards category; Hello again, I started writing a few lines of code and caught myself think 'Wow, hash tables are pretty nice, ...

  1. #1
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39

    Error when trying to implement a hash table class

    Hello again,

    I started writing a few lines of code and caught myself think 'Wow, hash tables are pretty nice, easy to write (havn't attacked index collision yet though.) and generally very cool stuff.' I hit a small snag though. First here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    class hashTable {
    	protected:
    		char **table;
    		
    		int keys;
    		//int **keysTable;
    		
    		unsigned int GenerateHash(char *string);
    		
    	public:
    		hashTable() {
    			keys = 0;
    		}
    	
    		int Insert(char *key, char *value);
    		
    		char *operator[] (char *);
    };
    
    char *hashTable::operator[] (char *key) {
    	return table[GenerateHash(key)];
    }
    
    int hashTable::Insert(char *key, char *value) {
    	int strSize = strlen(value);
    	unsigned int hash = GenerateHash(key);
    	
    	table[hash] = (char *)malloc(sizeof(char) * strSize);
    	strcpy(table[hash], value);
    	
    	//keysTable[keys] = (int *)malloc(sizeof(int));
    	//*keysTable[keys] = hash;
    	keys++;
    	
    	return hash;
    }
    
    unsigned int hashTable::GenerateHash(char *key) {
    	unsigned int hash = 0;
    	int size = strlen(key);
    	
    	for (int i = 1; i < size; i++) {
    		hash += (key[i] &#37; 251) * i;
    	}
    		
    	return hash;
    }
    
    int main() {
    	hashTable table;
    	int hash = table.Insert("key", "testing");
    	
    	printf("%s:%i = %s\n", "key", hash, table["key"]);
    	
    	return 0;
    }
    As soon as I uncomment 'int **keysTable' the program segfaults when I try to run it. If I uncomment that line and comment the 'int keys' line instead it dosn't segfault (uncommenting/commenting lines that depent on the variable).

    And I can't for the life of me figure out where the problem lies.
    Last edited by nempo; 10-06-2007 at 08:05 AM. Reason: Fixed a mistake in the error description.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    well, to begin with, neither 'keysTable' nor 'table' have been allocated any memory.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by Sebastiani View Post
    well, to begin with, neither 'keysTable' nor 'table' have been allocated any memory.
    I thought I was doing that in the Insert(...) function.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by nempo View Post
    I thought I was doing that in the Insert(...) function.
    You allocate space for the table elements in the insert member. But you never allocate space for the table.
    Kurt

  5. #5
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by ZuK View Post
    You allocate space for the table elements in the insert member. But you never allocate space for the table.
    Kurt
    hmz, could have sworn that doing something like that was allowed. Must have made a wrong assumption somewhere along the road.

    Thanks.

    Another question, how do you prevent the index generated from being larger then the largest allowable index, without putting a constraint on how you generate it. Or is this impossible forcing you to 'be smart' when doing so?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    hash_value &#37; table_size
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary in C# to hash table in C?
    By dinoman in forum C Programming
    Replies: 2
    Last Post: 04-12-2009, 09:23 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. hash table?
    By bennyboy in forum C Programming
    Replies: 2
    Last Post: 05-10-2007, 03:06 AM
  4. [Memory management] Hash table?
    By cboard_member in forum Game Programming
    Replies: 9
    Last Post: 04-15-2007, 01:52 PM
  5. Hash Table
    By siavoshkc in forum C++ Programming
    Replies: 4
    Last Post: 08-29-2006, 04:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21