gpt4 book ai didi

c++ - 执行简单哈希程序时出现运行时错误

转载 作者:行者123 更新时间:2023-11-28 06:36:07 26 4
gpt4 key购买 nike

我正在创建一个带有链接的简单散列程序,编译器没有给我任何错误,但程序本身在执行时崩溃了。该程序将搜索功能所用的时间差记录到一个文件中(数据集是 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;
}

最佳答案

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;

关于c++ - 执行简单哈希程序时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26736956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com