0

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;
}
1

check50 runs in a slightly different environment that can produce seg faults that won't show up in the IDE. However, the seg fault is showing that there's an error somewhere in the code. Also, it's possible that check50 is using inputs that will reveal an error that your testing doesn't.

Having said that, since every test is showing the seg fault, I'm thinking that there's a flaw in your load code. Without seeing the code, it's just a guess.

2
  • Thanks for your answer. I added some code and would be glad for some hint to point me in the right direction. – Lukas Apr 19 '16 at 5:43
  • I was having the exactly same problem. Thanks! – giovannisampaio May 22 '17 at 17:34

You must log in to answer this question.

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