code not compiling when i run it

#include <iostream>
#include <string>
#include <stdio.h>
#include "GeneralHashFunctions.h"


using namespace std;

struct NodeType
{
int x;
NodeType *next;
};

struct Node
{
/** member variables */
//int key ;
string key;
//string value ;
/** constructor initializes */
Node(string key) //, string value)

{
this->key = key;


//this->value = value ;
}
};

/** size of the array */
const int size = 40000 ;

/** An implementation of a simple hash table */

//------------------------HashTable Class----------------------
class HashTable {
Node *nodeArray[size];
public:

/** this hash function returns an index to the array */

/* int hashFunction(string key) {

for (int i=1; i<key.length(); i++)
key += key[i];
return (key%size) ;
}
*/ /** put the value into the array using an index generated using hash function */

void put(string key)
{
int index = DJBHash(key) ;
cout << index;
nodeArray[index] = new Node(key);

// creates pointers that will be used to build the linked list
NodeType *current = NULL, *prev = NULL, *head = NULL ;
for (int i=0; i < 8; i++) { // iterates eight times
current = new NodeType ; // creates a new node
current->next = NULL ; // let next point nowhere
current->x = i ; // assign value to member variable
if (head == NULL) { // if this is the first node
head = current ; // let head point there
}
else { // if this is not the first node
prev->next = current ; // let prev node point to this one
}
prev = current ; // let prev becomes the current node
}
}
};
//--------------------------------HashTable class End----------------------

this is a class and the build breaks at this line: nodeArray[index] = new Node(key);

Visual studio shows this at run time:
Unhandled exception at 0x0041aead in MiniSearchEngineProj.exe: 0xC0000005: Access violation writing location 0xbf435d74.
first use code tags [code]your code here[/code]

are you sure that the index is never out of bounds e.g. it's never -1 or so?
Thanks for your reply. I think you are right. My index is sometimes negative number and also it is quite number. So let me elaborate on what I am trying to achieve. I am writing code to read in the words from text files and save them as strings (token). Then I am trying to feed in these words to a Hash function and trying to generate indices to store these tokens as keys in hash table. My hash function is shown below. I am not understanding why it is not giving me correct indices. it shoudl take string as input. Any idea whats wrng here. thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
unsigned int DJBHash(const std::string& str)
{
   unsigned int hash = 5381;

   for(std::size_t i = 0; i < str.length(); i++)
   {
      hash = ((hash << 5) + hash) + str[i];
   }

   return hash;
}
/* End Of DJB Hash Function */

first DJBHash() results an unsigned int you should stick to it when you call it (like that unsigned int index = DJBHash(key) ;) since you get a large number by shifting it 5 times for each char in your string (is this intended?) and that leads when converting to signed to a negative number.

Even sticking to unsigned doesn't help since your hash can easily exceed the maximum size of your array of 40000. You have to limit it accordingly.
Topic archived. No new replies allowed.