General Protection Exception error.

Sep 9, 2018 at 2:54pm
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);
}
Sep 9, 2018 at 4:40pm
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 Sep 9, 2018 at 5:02pm
Sep 10, 2018 at 6:21am
@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
Sep 10, 2018 at 7:51am
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.
Sep 10, 2018 at 8:11am
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 Sep 10, 2018 at 8:22am
Sep 10, 2018 at 9:44am
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.