Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to hash english words from a txt file and store them in a table using chaining method . but i don't know what is wrong when i try to search and compare .
this Error :
"Exception thrown: read access violation.
search was 0x1.
If there is a handler for this exception, the program may be safely continued."


thanks.

What I have tried:

C++
#include <iostream>
#include <string> 
#include <sstream>
#include <fstream>
using namespace std;

#define a 1
#define b 2
#define p 311
#define size 355063

struct Node
{
	char* word;
	Node* next;
};
int ASCII_sum(char* word)
{
	int sum=0;
	for (int i = 0;i<strlen(word);i++) {="" sum="" +="word[i];" }="" return="" sum-2;="" int="" h(int="" k)="" (((a*k="" b)="" %="" p)="" size);="" void="" insert(node**="" d,="" index,="" node*="" word)="" if="" (d[index]="=" null)="" d[index]="word;" d[index]-="">next = NULL;
	}
	else
	{
		word->next = D[index];
		D[index] = word;
	}
}


Node** Hash(char* file_name)
{
	FILE* dictionary = fopen(file_name, "r");
	char w[128];
	Node** Dictionary = new Node* [size];
	for (int j = 0;j < size;j++) { Dictionary[j] = NULL; }
	int i = 0;
	while (i<354986)
	{
		fscanf(dictionary, "%s",w);
		Node* new_word = new Node;
		//new_word->word = strcpy(new_word->word, w);
		new_word->word = w;
		//insertion:
		int index = H(ASCII_sum(w));
		if (Dictionary[index] == NULL)
		{
			new_word->next = NULL;
			Dictionary[index] = new_word;
			
		}
		else
		{
			new_word->next = Dictionary[index];
			Dictionary[index] = new_word;
		}//insertion
		

		//insert(Dictionary, H(ASCII_sum(w)), new_word);
		//cout << Dictionary[index]->word<< endl;
		i++;
	}
	return &Dictionary[size];
}

/*void print_index(Node** table, int index)
{
	Node* P = table[index];
	while (P != NULL)
	{
		cout << P->word<<endl;
		P = P->next;
	}
}*/


bool check_sentence(string s,Node** Dectionary)
{
	bool found = true;
	istringstream split(s);
	while (split)
	{
		string tmp;
		split >> tmp;
		if (tmp == "") continue;
		char *t = new char[tmp.length()+1];
		t = strcpy(t, tmp.c_str());
		Node* search = Dectionary[H(ASCII_sum(t))];
		
		cout << H(ASCII_sum(t))<<endl;
		if (search == NULL)
		{
			return false;
		}
		while (search != NULL)
		{
			
			if (strcmp(search->word, t))
			{
				cout << search->word;
				found = true;
				break;
			}
			else
			{
				search = search->next;
				cout << search->word << endl;
			}
		}
		return false;
		
	delete [] t;	
	}
	
	return found;
}
void case_insensitive(char* text)
{
	return;
}
int main() {
	cout << "Processing...\n";
	Node* table = *Hash("words.txt");
	cout << "Finished.\n";
	string s;
	cin >> s;

	if (check_sentence(s,&table))
	{
		cout << "True\n";
	}
	else
	{
		cout << "False\n";
	}
}
Posted
Updated 3-Dec-16 0:27am
Comments
Afzaal Ahmad Zeeshan 2-Dec-16 16:21pm    
read access violation.

I think maybe this is a friendly version of saying that your program is accessing areas of memory it does not have access to.

Furthermore it also guides you by saying, search was 0x1.. You have a variable search. Check it again for 0x1; debug it here.
jeron1 2-Dec-16 16:30pm    
Sounds like a good time to step through the code with a debugger.

Assuming it is not homework, you should use std::unordered_map[^].
 
Share this answer
 
Quote:
"Exception thrown: read access violation.
search was 0x1.
If there is a handler for this exception, the program may be safely continued."
There is no way that a simple handling of the exception can allow your program to continue safely.
This message kindly tell you that you have a problem in 'pointer initializing', 'memory allocation' or 'pointer messing'. The problem can be a combination of the above.

The debugger is the best tool to track this kind of problem.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.

Nota: a bug in CP have altered your code.
 
Share this answer
 
v2
C++
fscanf(dictionary, "%s",w);
Node* new_word = new Node;
//new_word->word = strcpy(new_word->word, w);
new_word->word = w;

So every Node.word in your dictionary points to a temporary block of memory that belongs to the Hash function and will disappear as soon as that function returns. You need to allocate the memory for each word and copy the string from w, like:
C++
Node* new_word = new Node;
int size = strlen(w) + 1;  // size of required buffer
new_word->word = new char[size];
strcpy(new_word->word, w);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900