As a homework assignment, I'm making a C++ program that reads a text file and reports how many times each word appears.
Two requirements for the assignment are that we have to use hashes, and we have to use a vector with the indexes being the hashes.
So my program reads each word, then computes a hash (which will be a number less than 4096), then passes the hash and the word (as a string) to this function:
This function first checks whether the vector uses the hash yet. If it does, it checks to see if the word at that hash is the same as the one that was passed to the function. If it is, then it just increments the occurrence counter. If it isn't the same word, then it calls itself but with a hash 4096 greater.
If it doesn't find that the vector has used the hash, it inserts itself. That's what I would like it to do, anyways. Here's what I get for output:
(alice is the first word in the text, which is alice's adventures in wonderland.)
So I'm getting segmentation fault after it increases the capacity, but before it inserts the word, because it gets a segmentation fault instead.
Any suggestions as to why or how to fix it?
Two requirements for the assignment are that we have to use hashes, and we have to use a vector with the indexes being the hashes.
So my program reads each word, then computes a hash (which will be a number less than 4096), then passes the hash and the word (as a string) to this function:
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
typedef struct wordRec {
string word;
int occurances;
};
vector<wordRec *> words;
void foundWord (string word, unsigned int hash)
{
cout << "Over here! I have picked up " << word << endl;
if ((hash <= words.capacity()) && words[hash])
{
if (words[hash]->word == word)
{
words[hash]->occurances++;
cout << words[hash]->word << " has now been found " << words[hash]->occurances << " times." << endl;
}
else
{
foundWord (word, hash+4096);
}
}
else
{
struct wordRec * newWord = new wordRec();
newWord->word = word;
newWord->occurances = 1;
cout << "First time " << newWord->word << " has been found. Hash is " << hash << endl;
if (words.capacity() < hash)
{
cout << "I came here..." << endl;
words.reserve(hash+2);
cout << "And I won. Capcity is now " << words.capacity() << endl;
}
words.insert(words.begin()+hash, newWord);
cout << "It has been inserted." << endl;
}
}
This function first checks whether the vector uses the hash yet. If it does, it checks to see if the word at that hash is the same as the one that was passed to the function. If it is, then it just increments the occurrence counter. If it isn't the same word, then it calls itself but with a hash 4096 greater.
If it doesn't find that the vector has used the hash, it inserts itself. That's what I would like it to do, anyways. Here's what I get for output:
(alice is the first word in the text, which is alice's adventures in wonderland.)
Code:
Over here! I have picked up alice
First time alice has been found. Hash is 1504
I came here...
And I won. Capcity is now 1506
Segmentation fault: 11
So I'm getting segmentation fault after it increases the capacity, but before it inserts the word, because it gets a segmentation fault instead.
Any suggestions as to why or how to fix it?
Last edited: