0

Making my question more specific: My code is running and producing the same result as the staff's solution (checked all given .txt files with large dictionary and also successfully loaded small and one-word dictionaries, so no problem there). However, when I try check50, I am getting lots of seg faults. Check50 output:

:) 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 am using a trie, defined in dictionary.h

typedef struct node
{
    bool is_word;
    struct node *children[27]; //need 27 for a-z and apostrophe
} node;

node* root;

here the relevant parts of load and check:

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

Valgrind assures me there are no memory leaks. Any hints as to where my error lies are greatly appreciated. All my testing has not shown any problems, just check50 ...

0

The code you have posted doesn't allocate any memory for root, so it is immediately seg faulting when it tries to load the first word from the dictionary.

What I don't understand is how you are able to successfully run the program locally. Have you allocated the memory for root in a file other than dictionary.c? Maybe altered speller.c? Or is it possible that you've been building and executing one file but running check50 on a different version of the dictionary.c file?

2
  • Thanks Cliff, that was exactly the problem. I altered speller.c even though I shouldn't have. Allocing memory in dictionary.c instead fixed my problem. – Lukas Wampfler Apr 24 '16 at 9:14
  • I am also facing the same issue - my code is below : – Gayathri Sampath Apr 29 '16 at 8:43

You must log in to answer this question.

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