c++ - 哈希表段错误

标签 c++

所以我真的不确定是什么导致了 seg 错误,但我感觉它与 hashTables 有关,因为调试器在它涉及的这一行显示它的 seg 错误。

这是导致段错误的代码

//Constructor for hashtable
HashTable::HashTable()
{
    for(int i = 0; i < 10; i++)
    {
        hashTable[i] = new Movie;
        hashTable[i]->title = "empty";
        hashTable[i]->year = 0;
        hashTable[i]->next = NULL;
    }
}

//destructor for hashtable
HashTable::~HashTable()
{

}

// this will take a string and convert the letters to hash numbers then add them all together and divide by the hash table size to get a index
int HashTable::initHash(std::string in_title)
{
    int hashT = 0;
    int index = 0;

    for(int i = 0; i < in_title.length(); i++)
    {
        hashT = hashT + (int)in_title[i];
        std::cout << "hash = " << hashT << std::endl;
    }

    index = hashT % 10;
    std::cout << "index = " << index << std::endl;

    return index;
}

//This is where we will be inserting a new Movie into the hashtable, 
//it will first use initHash to find the number of where it should go in the hash and then from there add it to the hashtable
void HashTable::insertMovie(std::string in_title, int year)
{
    int index = initHash(in_title);
    std::cout << "index = " << index << std::endl;

    if (hashTable[index]->title == "empty") // *** seg faults right here ***
    {
        hashTable[index]->title = in_title;
        hashTable[index]->year = year;
    }

    else
    {
        Movie* Ptr = hashTable[index];
        Movie* n = new Movie;
        n->title = in_title;
        n->year = year;
        n->next = NULL;
        while(Ptr->next != NULL)
        {
            Ptr = Ptr->next;
        }
        Ptr->next = n;
    }
}

在我的每个包含 hashTables[index] 的函数中,它都会出现错误,但我不确定为什么,索引会返回一个应该有效的数字。有谁知道为什么会这样?

编辑:好的,这是我的头文件中重要的两个类

struct Movie{
    std::string title;
    int year;
    Movie *next;

    Movie(){};

    Movie(std::string in_title, int in_year)
    {
        title = in_title;
        year = in_year;
    }

};

class HashTable
{
    public:
        HashTable();
        ~HashTable();
        void insertMovie(std::string in_title, int year);
        int initHash(std::string in_title);
        int NumberofItemsInIndex(int index);
        Movie* findMovie(std::string in_title/*, int *index*/);
        void deleteMovie(std::string in_title);
        void printInventory();
        void PrintItemsInIndex(int index);
    protected:
    private:
        Movie **hashTable;
};

编辑 2:这是 main() 函数

int main(int argc, char * argv[])
{
    // Declarations
    int input; // Declaring an input for the menu
    bool quit = false; // Bool for the menu
    //string title; // input value for certain actions
    //int year; // input value for certain actions

    HashTable *ht;
    //int index;

    //readFileIntoHash(ht, argv[1]);



    while(quit != true)
    {
        displayMenu(); // Displays the main menu
        cin >> input;

        //clear out cin
        cin.clear();
        cin.ignore(10000, '\n');

        switch (input)
        {

        // Insert a movie
        case 1:
            {
                string in_title;
                int year;
                cout << "Enter Title:" << endl;
                cin >> in_title;
                cout << "Enter Year:" << endl;
                cin >> year;
                ht -> insertMovie(in_title, year);
                break;
            }

        // Delete a movie
        case 2:
            {
                string in_title2;
                cout << "Enter Title:" << endl;
                cin >> in_title2;
                ht -> deleteMovie(in_title2);
                break;
            }

        // Find a movie
        case 3:
            {
                string in_title3;
                cout << "Enter Title:" << endl;
                cin >> in_title3;
                ht -> findMovie(in_title3);
                break;
            }

        // Print table contents
        case 4:
            ht -> printInventory();
            break;

        case 5:
            cout << "Goodbye!" << endl;
            quit = true;
            break;

            // invalid input
        default:
            cout << "Invalid Input" << endl;
            cin.clear();
            cin.ignore(10000,'\n');
            break;
        }
    }
    return 0;
}

void displayMenu()
{
    cout << "======Main Menu=====" << endl;
    cout << "1. Insert movie" << endl;
    cout << "2. Delete movie" << endl;
    cout << "3. Find movie" << endl;
    cout << "4. Print table contents" << endl;
    cout << "5. Quit" << endl;
    return;
}

为了清楚起见,我添加了 displayMenu()。

最佳答案

修改类定义:

class HashTable
{
    public:
        HashTable();
        ~HashTable();
        void insertMovie(std::string in_title, int year);
        int initHash(std::string in_title);
        int NumberofItemsInIndex(int index);
        Movie* findMovie(std::string in_title/*, int *index*/);
        void deleteMovie(std::string in_title);
        void printInventory();
        void PrintItemsInIndex(int index);
    protected:
    private:
        Movie *hashTable[10]; /*<<-- since your size is fixed use an array*/
};

另外..因为你在构造函数中分配了电影,记得在析构函数中释放它们:

//destructor for hashtable
HashTable::~HashTable()
{
    for(int i = 0; i < 10; i++)
    {
        delete hashTable[i];
    }
}

替代方案,使用动态分配的内存(使用相同的类定义)

HashTable::HashTable()
{
    hashTable = new (Movie*) [10];
    for(int i = 0; i < 10; i++)
    {
        hashTable[i] = new Movie;
        hashTable[i]->title = "empty";
        hashTable[i]->year = 0;
        hashTable[i]->next = NULL;
    }
}

HashTable::~HashTable()
{
    for(int i = 0; i < 10; i++)
    {
        delete hashTable[i];
    }
    delete[] hashTable;
}

附加修复:

修改主函数:

int main(int argc, char * argv[])
{
    // Declarations
    int input; // Declaring an input for the menu
    bool quit = false; // Bool for the menu
    //string title; // input value for certain actions
    //int year; // input value for certain actions

    HashTable ht; // <<==== local variable, not a pointer!

... 然后将 ht->xxxx(...) 替换为 ht.xxxx(...) 其他地方。

关于c++ - 哈希表段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841799/

相关文章:

c++ - 使用索引几何处理法线

c++ - 在 C++11 中将方法附加到中断例程

c++ - 我们可以使用 boost::multi_index::multi_index_container 作为多索引映射吗?

c++ - "extern C++"是如何工作的?

c++ - 即使直接从官方示例复制,也找不到未知类型名称 QML_ELEMENT 和 QML 模块

c++ - MPIR gcc 编译 - 找不到 -lmpir

c++ - 在同一个 C++ 源文件中使用多个命名空间是一种好习惯吗?

c++ - 作为参数传递时如何复制值?

c++ - 指向结构对象的指针作为函数的参数 - 打印时出现奇怪的文本

c++ - 如何处理类层次结构