0

Have finished Pset5 and succesfully passed check50. Valgrind also shows no errors. However, when I want to submit for Big Board, to see how my program runs against those of others I get a error message:

Checking.............
:) dictionary.c and dictionary.h exist
    Checking that dictionary.c exists...
    Checking that dictionary.h exists...
    Checking that Makefile exists...
:) speller compiles
    running make...
    checking that program exited with status 0...
:( qualifies for Big Board
    valgrind tests failed; rerun with --log for more information.
    checking for valgrind errors... 
        Invalid read of size 1: (file: dictionary.c, line: 76)
:| passes benchmarking
    can't check until a frown turns upside down
See https://cs50.me/checks/91c97fdef90677c79c30fe012830c6d915f825ba for more detail

Here's the Valgrind log:

WORDS MISSPELLED:     13008
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        567967
TIME IN load:         2.79
TIME IN check:        12.99
TIME IN size:         0.00
TIME IN unload:       1.30
TIME IN TOTAL:        17.07

==17278== 
==17278== HEAP SUMMARY:
==17278==     in use at exit: 0 bytes in 0 blocks
==17278==   total heap usage: 224,632 allocs, 224,632 frees, 34,682,247 bytes allocated
==17278== 
==17278== All heap blocks were freed -- no leaks are possible
==17278== 
==17278== For counts of detected and suppressed errors, rerun with: -v
==17278== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Helping with...                                                                                                                                                      

==17278== All heap blocks were freed -- no leaks are possible
...
==17278== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Looks like your program doesn't have any memory-related errors! Be sure, though, to test it with other inputs!

Does anyone know where this problem might come from?

======================================================

Here's the code of the part where the error occurs (line 76):

// If word is two chars long
else if(w == 2)
    {
        if(root->lengthword_bc[w] == NULL)
        {
            return false;
        }

        else if(root->lengthword_bc[w] != NULL)
        {
            travfirst = root->lengthword_bc[w];
        }

        // Calculate hash code for first two letters and update if second letter is apostrophe
        int c1 = (27 * ((tolower(word[0])) - 97)) + (1 * ((tolower(word[1])) - 97));

        if(word[1] == '\'')
        {
            c1 = (27 * (word[0] - 97)) + 26;
        }

// Line 76
        if(travfirst->combi1_b[c1] != true)
        {
            return false;
        }

        else
        {
            return true;
        }

Running ./speller texts/aca.txt > student.txt revealed that the error is caused by trying to access an array with element [-838]:

dictionary.c:76:12: runtime error: index -838 out of bounds for type 'bool [729]'

that means that the line where I calculate c1 (c1 is the hash code, corresponding to the combination of the first chars of the word being checked), for this word results in c1 getting the value -838:

// Calculate hash code for first two letters and update if second letter is apostrophe
        int c1 = (27 * ((tolower(word[0])) - 97)) + (1 * ((tolower(word[1])) - 97));

But, this function only takes alphabetical chars and the apostrophe char as input, so How can the result of this function then be -838?

c1 is then used in this line:

if(travfirst->combi1_b[c1] != true)
        {
            return false;
        }

To check whether variable combi1_b[c1] (a boolean variable) in struct 'first2chars' pointed to by 'travfirst' is not equal to true. If so that means that this word is not loaded into the dictionary and therefore check returns false.

3

checking for valgrind errors... Invalid read of size 1: (file: dictionary.c, line: 76)

So what's line 76 on dictionary.c?

Also, based on the time your spell-checker took for that one text, I can be fairly confident that after fixing any valgrind errors, you won't qualify for the Big Board with your current implementation. The Big Board runs all 30 texts and sums together the times. If it's more than about 20-25 seconds total it will time out.

I'd suggest trying to get your total time for that file (tolstoy.txt) to under 2 seconds before submitting to the Big Board.


Based on your edit,

c1 = (27 * (word[0] - 97)) + 26

what happens if word[0] is uppercase here? You'll get a negative value for c1.

10
  • Thanks. Regarding the Big Board: I already handed in an earlier version of this program, and that did succeed. I hope the adjustments will make it a bit faster, since I only made some simplifications. Running Valgrind makes my speller check a lot slower, without it is just slightly above 2 secs. But do you have any idea how Valgrind on several texts does not find any memory leak, but test for Big Board does?
    – JJuice
    Feb 11 '18 at 22:48
  • @JanJoosse ooh, sorry, I forgot about valgrind's affect on the speed! What's line 76? Is it part of load or check? I'd guess check given that it only happens with some texts. Do you have enough space for the longest word? Have you tested with texts/pneumonoultramicroscopicsilicovolcanoconiosis.txt
    – curiouskiwi
    Feb 12 '18 at 1:17
  • I have added more details above.
    – JJuice
    Feb 12 '18 at 19:53
  • @JanJoosse I don't see any changes. Did you save?
    – curiouskiwi
    Feb 12 '18 at 20:04
  • sorry, just did
    – JJuice
    Feb 12 '18 at 20:11

You must log in to answer this question.

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