0

I've been working on Speller for a while now, but I've been stuck on this same problem. When testing my code by running ./speller texts/ANYOFTHETEXTS, it says I load 143092 words, but it also always says 0 words from the texts are misspelled. When I run help50 valgrind, I get this:

    WORDS MISSPELLED:     0
    WORDS IN DICTIONARY:  143092
    WORDS IN TEXT:        6
    TIME IN load:         1.65
    TIME IN check:        0.00
    TIME IN size:         0.00
    TIME IN unload:       0.19
    TIME IN TOTAL:        1.84

    ==2480== 
    ==2480== HEAP SUMMARY:
    ==2480==     in use at exit: 552 bytes in 1 blocks
    ==2480==   total heap usage: 143,097 allocs, 143,096 frees, 8,023,472 bytes allocated
    ==2480== 
    ==2480== 552 bytes in 1 blocks are still reachable in loss record 1 of 1
    ==2480==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==2480==    by 0x5258E49: __fopen_internal (iofopen.c:65)
    ==2480==    by 0x5258E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
    ==2480==    by 0x4011F1: load (dictionary.c:82)
    ==2480==    by 0x4009B4: main (speller.c:40)
    ==2480== 
    ==2480== LEAK SUMMARY:
    ==2480==    definitely lost: 0 bytes in 0 blocks
    ==2480==    indirectly lost: 0 bytes in 0 blocks
    ==2480==      possibly lost: 0 bytes in 0 blocks
    ==2480==    still reachable: 552 bytes in 1 blocks
    ==2480==         suppressed: 0 bytes in 0 blocks
    ==2480== 
    ==2480== For counts of detected and suppressed errors, rerun with: -v
    ==2480== ERROR SUMMARY: 124 errors from 6 contexts (suppressed: 0 from 0)

    Asking for help...

    ==2480== Conditional jump or move depends on uninitialised value(s)

    Looks like you're trying to use a variable that might not have a value? Take a closer look at line 111 of dictionary.c.

Here is my load function

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary){

//Open dictionary file//
FILE *file = fopen(dictionary, "r");

//Check if file loaded is empty
if(file == NULL)
{
    printf("Could not load dictionary.\n");
    return false;
}

//Character aray of max size 45 + 1
char buffer[46];

//Read strings from file one at a time//
do
{
    //Create a new node for each word//
    node *new_word = malloc(sizeof(node)); //Allocate memory for a node pointer

    //Check if you have enough memory to malloc
    if(new_word == NULL)
    {
        printf("Not enough memory.\n");
        return false;
    }

    //Copy word into node
    strcpy(new_word->word, buffer);           ////***Valgrind finds an error here***////

    //Hash word to obtain hash value//
    int hash_value = hash((const char *)buffer);

    //Insert node at table[hash_value]//
    if(table[hash_value] == NULL)
    {
        table[hash_value] = new_word;
    }
    else
    {
        new_word->next = table[hash_value];
        table[hash_value] = new_word;
    }

    word_counter++;

}
while (fscanf(file, "%s", buffer) != EOF);

if(feof(file))
{
    return true;
}

return false;
}

Any help would be appreciat

1

First of all, while valgrind might give clues to problems, you should always get the code running correctly before trying to resolve memory leaks or other valgrind issues. Frequently, logic errors and other code bugs actually cause valgrind issues that usually disappear when the logic errors are resolved.

The valgrind error of uninitialized value happens because the code uses a do/while construct instead of a plain while loop. Specifically, the first pass through the loop has not read anything into buffer, so it's uninitialized. This is definitely causing a problem. Instead, a plain while loop will do the read before trying to process any data and should resolve this issue.

As for the spell check, the first problem is that when a new node is created, the new_word->next element is not being initialized by setting it to NULL. This is likely having some effect.

I'm also suspicious that there are some issues with the check function because you're saying that no words are being returned as misspelled.

I suggest creating two small files, one as a small dictionary and one as a small text file to be checked. The check file should include some words from the dictionary, all lower case, a couple words with capital letters but matching an all lower case word in the dictionary and a couple words that are totally not in the dictionary, no matter the case.

This, however should resolve your valgrind questions. Any new problems warrant a new question.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • You were right about the while loop correction. It never occurred to me that there was a distinction between the processes of a do-while and a plain while loop, thanks!
    – lmcc
    Jun 19 '20 at 15:26

You must log in to answer this question.

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