0

Below is the code from my spellchecker load procedure; other parts of the code omitted. When I use a small dictionary, everything seems to run fine, but when I try to load the large dictionary, I get a Segmentation Fault error.

I have tried running valgrind, and it points at the four lines where I use the command

currentNode -> children[letterIndex]

but I don't understand why what I am doing is wrong, especially since it works with a smaller dictionary that I am testing.

unsigned int wordCount = 0;

typedef struct node
{
     bool isAWord;
     struct node* children[numChildren];
}
node;

struct node* rootNode;

bool load(const char* dictionary)
{
FILE* fp = fopen(dictionary, "r");  //Opening up the dictionary
if (fp == NULL)
{
    printf("Could not open %s.\n", dictionary);
    return false;
}

rootNode = calloc (1, sizeof(node));   //setting up the rootNode; calloc clears the memory as well as allocating
struct node* currentNode = rootNode;
int letterIndex = 0;

for (int c = fgetc(fp); c != EOF; c = fgetc(fp))   //stepping through the whole file, like in speller
{
    if (isalpha(c) || c =='\'')   // if it's a letter or an apostrophe
    {
        letterIndex = c - '\'';   //sets index to 0 if apostrophe, 1-26 for the letters 
        if (currentNode -> children[letterIndex] == NULL)
        {
            currentNode -> children[letterIndex] = calloc (1, sizeof(node));  //create a new node and go there
            currentNode = currentNode -> children[letterIndex];
        }
        else
        {
            currentNode = currentNode -> children[letterIndex];    //just go there
        }
    }
    else //not a letter/apostrophe, so end of the line; set isAWord true, increment wordcount, and make current = root
    {
        currentNode -> isAWord = true;
        wordCount++;
        currentNode = rootNode;
    }
}

printf("\n%i\n", wordCount);   //Just for testing - make sure to remove when submitting

fclose(fp);   //close down the dictionary

// TODO
return true;
}

The dictionary list that I am using is: cat caterpillar dickie dickie's pinochle emuification delta foxtrot hotel hotel's meatloaf

I don't know if that makes a difference, but I'm trying to be thorough. Any help/suggestions would be greatly appreciated, because I'm also having a segfault problem with my check function, and I think I need to get this one fixed first.

Thanks,

-Brian

2
  • what's numChildren? is it defined anywhere?
    – Chris Shi
    Jul 27 '16 at 20:26
  • Yes, I forgot to include that part. It's defined as 27; 26 slots for each letter of the alphabet and one more for the apostrophe. Jul 28 '16 at 0:14
3

For this line:

 letterIndex = c - '\''; 

try to see what happens if c == 'a', the ASCII code for '\'' is 39. Try fixing this and see what happens

5
  • Oh, man - I was reading the ASCII table wrong. I thought that 96 was the apostrophe. I'll try that and report back. Jul 28 '16 at 0:17
  • cool, lemme know how it goes
    – Chris Shi
    Jul 28 '16 at 0:33
  • That completely fixed that problem. Thanks so much - I was banging my head against the wall. Now I'm going to keep testing it. Jul 28 '16 at 16:14
  • 3/4 working properly now - that was the big bug. Now I've just got to work on my unload. Thanks again. Jul 28 '16 at 16:20
  • No problem. Glad I could help you out
    – Chris Shi
    Jul 28 '16 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .