【问题标题】:Runtime error while executing simple hashing program执行简单哈希程序时出现运行时错误
【发布时间】:2014-11-04 13:57:12
【问题描述】:

我正在创建一个带有链接的简单哈希程序,编译器没有给我任何错误,但程序本身在执行时崩溃了。该程序将搜索功能所采用的时间差记录到一个文件中(数据集是 1000 个随机生成的数字的 x% 散列)(我不知道它是否与逻辑本身有关,还是一些技术错误。我使用的是 pocketcpp 作为,错误..ide)。代码:

#include <fstream>
#include <iostream>
#include <chrono>
#include <ctime>
#include <cstdlib>
#include <cstring>

/*Code by RN:121503 * Date: 04-11-2014*/

using namespace std;

typedef struct storagelist
{
    int value;
    storagelist *next;
};

storagelist *startArray[100];

int simplehash(int n)
{
    int index;
    index=n%1000;
    return index;
}

int simplesearch(int n)
{
    int index;
    int i=0;
    storagelist *tempPointer=startArray[n]->next;
    index=simplehash(n);
    //Base address of array, i.e. array index will be null.
    if(startArray[n]->next==NULL)
    {
        //cout<<"No such key/value exist \n";
    }
    else
    {
        do
        {
            if(tempPointer->value==n)
            {
                //cout<<"Key found \n";
                break;
            }
            else
            {
                tempPointer=tempPointer->next;
            }
        }while(tempPointer->next!=NULL);
    }
}

void simpleinsert(int n)
{
    int tempIndex;
    storagelist *tempPointer;
    storagelist *tempPointertwo;
    tempIndex=simplehash(n);
    if(startArray[tempIndex]->next==NULL)
    {
        tempPointer=new storagelist;
        tempPointer->value=n;
        startArray[tempIndex]->next=tempPointer;
        tempPointer->next=NULL;
    }
    else
    {
        while(tempPointer->next!=NULL)
        {
            tempPointer=tempPointer->next;
        }
        tempPointertwo=new storagelist;
        tempPointertwo->value=n;
        tempPointer->next=tempPointertwo;
        tempPointertwo->next=NULL;
    }
}

int main()
{
    int tempRandom;
    int loopHelp;
    int randomBackup[100];
    //File handeling
    ofstream myfile;
    //Clock elements
    clock_t start, end;
    //Populating starting indices with NULL
    for(int i=0;i<100;i++)
    {
        startArray[i]->next=NULL;
    }
    /*generating 1000 random numbers between 1 to 10000
    and storing+hashing them*/
    srand (time(NULL));
    for (int k=0;k<100;k++)
    {
        tempRandom=rand()%10000+1;
        randomBackup[k]=tempRandom;
        simpleinsert(tempRandom);
    }
    //following are search cases with time logging
    for(int m=1;m<10;m++)
    {
        switch(m)
        {
        case 1:
            myfile.open ("10per.txt");
            for(loopHelp=0;loopHelp<(100*(10/100));loopHelp++)
            {
                start = clock();
                simplesearch(randomBackup[loopHelp]);
                end = clock();
                myfile<<loopHelp<<"\t"<<(double)(end-start)<<"\n";
            }
            break;
        case 2:
            myfile.open ("20per.txt");
            for(loopHelp=0;loopHelp<(100*(20/100));loopHelp++)
            {
                start = clock();
                simplesearch(randomBackup[loopHelp]);
                end = clock();
                myfile<<loopHelp<<"\t"<<(double)(end-start)<<"\n";
            }
            break;
        case 3:
            myfile.open ("30per.txt");
            for(loopHelp=0;loopHelp<(100*(30/100));loopHelp++)
            {   
                start = clock();
                simplesearch(randomBackup[loopHelp]);
                end = clock();
                myfile<<loopHelp<<"\t"<<(double)(end-start)<<"\n";
            }
            break;
        case 4:
            myfile.open ("40per.txt");
            for(loopHelp=0;loopHelp<(100*(40/100));loopHelp++)
            {
                start = clock();
                simplesearch(randomBackup[loopHelp]);
                end = clock();
                myfile<<loopHelp<<"\t"<<(double)(end-start)<<"\n";
            }
            break;
        case 5:
            myfile.open ("50per.txt");
            for(loopHelp=0;loopHelp<(100*(50/100));loopHelp++)
            {
                start = clock();
                simplesearch(randomBackup[loopHelp]);
                end = clock();
                myfile<<loopHelp<<"\t"<<(double)(end-start)<<"\n";
            }
            break;
        case 6:
            myfile.open ("60per.txt");
            for(loopHelp=0;loopHelp<(100*(60/100));loopHelp++)
            {
                start = clock();
                simplesearch(randomBackup[loopHelp]);
                end = clock();
                myfile<<loopHelp<<"\t"<<(double)(end-start)<<"\n";
            }
            break;
        }
    }
    return 0;
}

【问题讨论】:

  • 如果这是 Visual Studio,则启用调试器异常以在访问冲突时中断,并在它停止时将调用堆栈向上移动到您的代码。
  • 更正了,至于 ide,我实际上是在使用带有 gcc 和 minigw 的 notepad++
  • 您应该首先研究分配startArray 中的指针指向的代码。
  • 更正了插入过程中的一个小错误,即我没有为临时指针分配任何起始地址,还更正了索引分配错误的搜索功能。尽管如此,运行时错误。

标签: c++ hash runtime chaining


【解决方案1】:
storagelist *startArray[1000];
[...]
int main()
{
[...]
    for(inti=0;i<1000;i++)
    {
        startArray[i]->next=NULL;

运行时错误是由未初始化的 startArray[i] 导致的,它只包含 NULL 指针。我猜 NULL 指针没有您可以在运行时访问的成员 'next',这会导致分段错误。

所以你错过的是创建实际项目并将它们存储在你的 startArray 中:

    for(inti=0;i<1000;i++)
    {
        storagelist *item = new storagelist;
        item->value = 0;
        item->next = NULL;
        //startArray[i]->next=NULL;

【讨论】:

  • 这就是问题所在,我只是将指针数组误认为是对象数组。我想知道为什么编译器没有给我任何错误。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2017-06-28
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多