Search code examples
cstack-overflow

For a small dictionary list program runs, but for a large list it gives an error


I am new to C and in one assignment I have to load a 141,000 word dictionary in computer memory.

Below code works when loading a smaller dictionary (I tried up to 700 words) but when loading a bigger dictionary compiler gives segmentation error.

On running debugger, I can see call stack does not have next function (main) in queue when loading 140k word dictionary error on 140k word list. But for small dictionary with 700 words call stack has the next function (main) in the queue ready for execution the call stack is ok with small list

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char inword[1]; // to store word
    FILE *infile = fopen(dictionary, "r"); // open file location is in definations of speller.c
    // check if filepointer worked
    if (infile == NULL)
    {
        fprintf(stderr, "Could not open DICTIONARY");
        return false;
    }
    //loop over each word
    while (fscanf(infile, "%s", inword) != EOF)
    {
        node *temp;
        //save word in node
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            fprintf(stderr, "Could not malloc");
            return false;
        }
        strcpy(n->word, inword);
        n->next = NULL;
        // hash word to obtain hash value
        int hashn = hash (inword);
        // insert the word into hash table
        if (table[hashn] == NULL)
        {
            n->next = NULL;
        }
        else
        {
            n->next = table[hashn];
        }
        table[hashn] = n;
        count++;
    }
    fclose(infile);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    //  printf("xxThe size of dictionary loaded is : %i\n", count); // for testing
    return count;
}

Solution

  • inword[1] is only big enough to store an empty string.

    // char inword[1]; // to store word
    char inword[100];
    

    Use width limits. Test positively for expected result.

    // while (fscanf(infile,"%s",inword) != EOF)
    while (fscanf(infile,"%99s",inword) == 1)
    

    I suspect unposted node may need re-work too.