【问题标题】:Open address hashtable segmentation fault打开地址哈希表分段错误
【发布时间】:2014-10-31 23:35:53
【问题描述】:

我正在尝试创建一个开放寻址的哈希表。用户必须能够定义表格的大小。对于我的碰撞解决方案,我正在使用伪随机探测 -

http://algoviz.org/OpenDSA/Books/OpenDSA/html/HashCImproved.html#HashingPseudoRandomProbePRO

无论如何,哈希函数似乎都可以正常工作,直到我只有 1 个空间可以填充表格。一旦我输入表格的最后一个元素,我的程序就会崩溃,并且出现分段错误。几个小时以来,我一直在努力寻找问题。请帮忙!

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;

class HashTable
{

    string* hash;
    bool* inTable;
    vector<int> perm;
    int table_size;
    int permFunc(int, int);
    void permutation();
    int hashFunc(string);

public:

    //constructor for a hashtable
    HashTable(int);

    //deconstructor for hashtable
    ~HashTable();

    bool isFull();

    //The prototype for the insert method
    void insert(string);

     //The prototype for the lookup method
    bool lookup(string);

    //The prototype for the remove method
    void remove(string);

    //The prototype for the size method
    int size();

    //The prototype for the print method
    void print();

};

//constructor

HashTable::HashTable(int table_size)
{
    this -> table_size = table_size;
    hash = new string[table_size];
    inTable = new bool[table_size];
    for(int i=0; i<table_size; i++) perm.push_back(i);
    permutation();
}

//deconstructor
HashTable::~HashTable()
{
    delete [] hash;
    delete [] inTable;
}

//creates a permutation of the perm vectors index elements
void HashTable::permutation()
{
    random_shuffle ( perm.begin() + 1, perm.end() );
}

//Hash function method for strings
int HashTable::hashFunc(string str)
{
    int key = 0;
    for (int i=0; i < str.size(); i++)
    {
        key += static_cast<int>(str.at(i));
    }
    cout << "key is:" << key << endl;
    return key%table_size;
}

int HashTable::permFunc(int index, int i)
{
        int temp = index;
        index += perm[i];
        if(index > table_size){index = index - table_size;}
        if(inTable[index] == false ){return index;}
        index = temp;
        i++;
        permFunc(index, i);
}

bool HashTable::isFull()
{
    int num=0;
    for(int i=0;i<table_size; i++)
    {
        if(inTable[i] == true){num ++;}
        if(num == table_size){
            cout<<"FULL TABLE";
            return true;}
    }
    return false;
}

//Method that inserts an integer into the hash table
void HashTable::insert(string str)
{
    int i=1;
    if(isFull()){return;}

    int pos = hashFunc(str);
    if(inTable[pos] == true)
    {
        int permPos = permFunc(pos, i);
        hash[permPos] = str;
        inTable[permPos] = true;
    }
    else
        hash[pos] = str;
        inTable[pos] = true;
}
//A function that prints out the menu
void menu()
{
    cout << endl;
    cout << "press i to insert an element into the hash table" << endl;
    cout << "press d to inTableete an element from the hash table" << endl;
    cout << "press l to look up an element" << endl;
    cout << "press s to obtain the size of the table" << endl;
    cout << "press p to print the current table" << endl;
    return;
}

//The main method
//Implements all the input and output
int main(void)
{
    int table_size;
    cout << "Enter table size: ";
    cin >> table_size;

    HashTable h(table_size);
    while(true)
    {
        char c;
        string str;
        menu();
        getline(cin, str);
        stringstream stream;
        stream << str;
        stream >> c;
        switch(c)
        {
            case 'i':
                getline(cin, str);
                h.insert(str);
                break;
            case 'd':
                getline(cin, str);
                h.remove(str);
                break;
            case 'l':
                getline(cin, str);
                cout << h.lookup(str) << "\n";
                break;
            case 's':
                cout << h.size() << "\n";
                break;
            case 'p':
                h.print();
                break;
            default:
                cout << "Don't understand '" << c << "'\n";

        }
    }
    return 0;
}

【问题讨论】:

    标签: c++ segmentation-fault hashtable


    【解决方案1】:

    我不知道这是否是你的问题,但这一行:

    if(index > table_size){index = index - table_size;}
    

    会将index 留在table_size,这超出了您的数组的范围。将&gt; 更改为&gt;=

    如果不是这样,您能否在帖子中添加一个示例崩溃输入以进行测试?

    【讨论】:

      猜你喜欢
      • 2016-03-03
      • 2012-04-23
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2011-02-03
      • 2011-11-17
      • 2014-03-28
      相关资源
      最近更新 更多