Monday 15 June 2015

c++ - Runtime error while executing simple hashing program -



c++ - Runtime error while executing simple hashing program -

i creating simple hashing programme chaining, compiler didn't give me error, programme crashing @ time of execution. programme logs time difference taken search function file(the dataset x% of 1000 randomly generated numbers hashed)(i don't know if logic itself, or technical error. using pocketcpp as, err..ide). code :

#include <fstream> #include <iostream> #include <chrono> #include <ctime> #include <cstdlib> #include <cstring> /*code 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; homecoming 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 null. if(startarray[n]->next==null) { //cout<<"no such key/value exist \n"; } else { { 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 null for(int i=0;i<100;i++) { startarray[i]->next=null; } /*generating 1000 random numbers between 1 10000 , storing+hashing them*/ srand (time(null)); (int k=0;k<100;k++) { temprandom=rand()%10000+1; randombackup[k]=temprandom; simpleinsert(temprandom); } //following search cases 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; } } homecoming 0; }

storagelist *startarray[1000]; [...] int main() { [...] for(inti=0;i<1000;i++) { startarray[i]->next=null;

the runtime error results uninitialized startarray[i], contains null-pointers. guess null-pointer has no fellow member 'next' can access @ runtime, results in segmentation fault.

so miss creating actual items , store them in startarray:

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

c++ hash runtime chaining

No comments:

Post a Comment