General Protection Exception error.

Hi! I was trying to make a hashing program. There was no error while compiling...but when I run this I get an 0X2507:0X00DA Processor fault.
What is this error and how to rectify this.

Also this is my first try on hashing algorithms, please suggest improvements if any. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void hash(char string[], int size)
{
	char hash[10];
	int i;	char alpha;
	for(i=0;i<size;i++)
	{
		alpha=string[i];
		srand(int(alpha));
		hash[i]=rand();
	}
	cout<<"\nHash: "<<hash;
}

void main()
{
	char text[10]; int rem, len, i;
	cout<<"Enter text till 10 char. Only alpha numeric:\n";
	gets(text);
	len=strlen(text);
	if(len<10)
	{
		rem=10-len;
		for(i=0;i<rem;i++)
		{
			text[10-i]='a';
		}
	}
	hash(text, len);
}
text[10-i]='a';

When i=0 , this will write to text[10] which does not exist.

rand() returns an int, but then you're trying to store that int as a char. A char can hold only 256 different values, but rand() can return many more than that. What are you expecting to happen when rand() return the number 10234 and you try to store than in a char?
Last edited on
@Repeater Can I somehow make rand() return a single digit value or are there any other functions which returns same single digit random numbers for same seed
int singleDigitValue = rand() % 10;

I would be remiss if I did not say that using srand is bad, using rand is bad, you shouldn't be doing it. However, it looks like you're using a compiler from thirty years ago (you know that new compilers are given away for free, yes?), so it's astonishing that you're able to make it work at all.
Your hash function; there's no need for the rand section at all. All you're doing is mapping the first ten letters, exactly the same every time. Your hash function might as well just be the first ten letters.
Last edited on
Also, if you're going to build a char array to be displayed, you need to put a zero at the end. Otherwise how does cout know when to stop outputting?
Topic archived. No new replies allowed.