我正在用鏈創建一個簡單的哈希程序,編譯器沒有給我任何錯誤,但程序本身在執行時崩潰。程序將搜索函數所花費的時間差記錄到一個文件中(數據集是1000個隨機生成的數字哈希的x%)(我不知道它是否與邏輯本身有關,或者是某種技術錯誤,我使用pocketcpp作爲,err..ide)。代碼:從所述未初始化startArray [I],其中僅包含NULL指針執行簡單哈希程序時運行時錯誤
#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中啓用調試異常的訪問衝突斷裂,當它停止行走了調用堆棧,以你的代碼。 – drescherjm 2014-11-04 14:03:58
更正了這一點,至於ide,我實際上是使用記事本+ + gcc和minigw – Siddharth 2014-11-04 14:08:27
你應該首先研究分配'startArray'指向的指針的代碼。 – molbdnilo 2014-11-04 14:21:33