Hash Table Bug

closed account (oEwqX9L8)
I'm working on a hash table project using chaining to resolve collisions and I have a weird bug I can't figure out. Adding multiple items to one index works unless they are the only items in the whole hash table. If I try and add two items to one index on an otherwise empty table I get a seg fault. Weirdly I don't always get the seg fault. Here is my hash function, my rehash function and my add function.

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
  uint hashItem(string value) {
    uint hashIndex = 0;
    for (int i = 0; i < value.size(); i++) {
      hashIndex *= 31;
      hashIndex += value[i];
    }
    return hashIndex % capacity;
  }

  void rehashTable(uint newCapacity) {
    if (newCapacity == 0) {
      clear();
      return;
    }
    LinkedList<ItemType>* temp = new LinkedList<ItemType> [newCapacity];
    uint oldCapacity = capacity;
    capacity = newCapacity;
    for (int i = 0; i < oldCapacity; i++) {
      for (int q = 0; q < table[i].getSize(); q++) {
        temp[hashItem(table[i].getItem(q))].insertItem(table[i].getSize(), table[i].getItem(q));
      }
    }
    delete[] table;
    table = temp;
  }

  void addItem(const ItemType& item) {
    if (!findItem(item)) {
      ++num_items;
      if (num_items > capacity) {
        rehashTable((capacity*2)+1);
      }
      uint index = hashItem(item);
      table[index].insertItem(table[index].getSize(), item);
    }
  }


Could anyone give me an idea of which part is causing the seg fault? Thanks for any pointers!
Use a debugger.
Topic archived. No new replies allowed.