working on pset5's spell checker: load, check and unload seem to be working fine, such that there are no differences from my output to the staff's using >diff ... and all the provided text files. However, when checking via
check50 2015.fall.pset5.speller dictionary.c dictionary.h Makefile
I am receiving a bunch of frowny faces because of seg faults:
:) dictionary.c, dictionary.h, Makefile exist
:) speller compiles
:( handles most basic words properly
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 193..."
:( handles min length (1-char) words
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 191..."
:( handles max length (45-char) words
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 191..."
:( spell-checking is case-insensitive
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 192..."
:( handles possessives properly
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 192..."
:( handles substrings properly
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 193..."
https://sandbox.cs50.net/checks/dbf13e9eb91845408b1ba1c0e7ad2024
I have no idea where this is coming from and I can't make any sense of these error messages. I am using a trie for the dictionary and have never gotten a seg fault so far when running >>./speller texts/....
Checking my code with valgrind, all seems to be working fine (freeing up all memory, no errors).
Glad for any inputs you might have.
bool check(const char* word)
{
int index = 0;
node * cur_node = root;
char c ='a';
int c_index;
while(c!='\0')
{
c = word[index];
c_index = (c == '\'') ? 26 : tolower(c)-'a';
if(cur_node->children[c_index] == NULL)
{
return false;
}
else
{
cur_node = cur_node->children[c_index];
}
index ++;
c = word[index];
}
if(cur_node->is_word)
{
return true;
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
node * cur_node = root;
FILE* fp = fopen(dictionary, "r");
if (fp == NULL)
{
printf("Could not open %s.\n", dictionary);
return 1;
}
int index = 0;
int c = fgetc(fp);
while(c != EOF)
{
while(c!='\n' && c != EOF)
{
index = (c == '\'') ? 26 : tolower(c)-'a';
if (cur_node->children[index]==NULL)
{
node * new_node = malloc(sizeof(node));
if(new_node == NULL)
{
printf("Unable to allocate memory\n");
return 0;
}
else
{
new_node->is_word = 0;
for(int i=0; i<27; i++)
{
new_node->children[i]=NULL;
}
cur_node->children[index] = new_node;
cur_node = new_node;
}
}
else
{
cur_node = cur_node->children[index];
}
c = fgetc(fp); // read next character
}
cur_node->is_word=1;
size_of_dict ++;
cur_node = root;
c = fgetc(fp);
}
fclose(fp);
return true;
}