-4

I know the question has been posted but I couldn't relate the answers to my problem unfortunately so I will ask again...

I am working on linear hashing project. My code should basically create a hash table which grows dynamically. Elements are stored in buckets of fixed size (arrays) and then there are more buckets in the table. Every bucket can have its overflow bucket (created when there is no more place in the original bucket) and each time an overflow is created so called 'split' is done and one more bucket is added to the table and the table is rehashed. There are more details but I think these basics are enough to understand my code..

So I have data structures element and bucket and bucket has several methods of its own, to insert element, find it, create overflow, etc. Further on the class that everything else is happening in is ADS_set, its a template class with pre-described methods that I have to use. The whole thing works with pointers and I have several test-files that I use for testing it.

When I test the code now, it compiles but at some point it returns following error message: caught signal: floating point exception, with a hint: method: insert(iterator,iterator) which is as far as I understand this:

template<typename InputIt> void insert(InputIt first, InputIt last) {                        
    for (auto it=first; it != last; ++it) {
        size_type idx = hashIndex(*it); 
        if(!(table[idx]->findElement(*it))) {
              insertElementInTable(*it);
        }
     }
}

This part of code works with hash function which does use a modulo but I cannot see how this would cause the floating point exception, as the hash function is this:

size_type hashIndex(const_reference key) const {
    size_type idx = hasher{}(key) % (2^roundNum); //roundNum is initialized with 1
    size_type d{roundNum};

    if(idx < nextToSplit) {
        ++d;
        idx = hasher{}(key) % (2^d);
    }

    return idx;
}

The rest of code you can see here https://pastebin.com/HXeUpaW5 and most important functions are:

--> findElement(const_reference key) - lines 51-69

--> insertElementInTable(const_reference key) - lines 179-197 which calls createOverflow() 44-49, splitTable() 131-147, rehashAfterSplit() 153-176 and insertElementInBucket() 72-88

Hope You can give some new insights, cause I don't have any ideas why the problem occurs. Thanks!

selma
  • 13
  • 7

1 Answers1

2

In C++, ^ is the bitwise XOR operator, not the power operator. You are dividing by zero.

Nikola Benes
  • 2,372
  • 1
  • 20
  • 33