0

Hi~ Could you please tell me the reason I can’t pass CS50 check? It seems there is no error when I run valgrind ./speller texts/lalaland.txt (I count the nodecount q’ty during LOAD and confirmed the nodecount q’ty is 0 after UNLOAD.)

The result of valgrind and check50 are as following:

==21620== ==21620== HEAP SUMMARY:

==21620== in use at exit: 0 bytes in 0 blocks

==21620== total heap usage: 367,084 allocs, 367,084 frees, 82,227,504 bytes allocated

==21620== ==21620== All heap blocks were freed -- no leaks are possible

==21620==

==21620== For counts of detected and suppressed errors, rerun with: -v ==21620== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Helping with...

==21620== All heap blocks were freed -- no leaks are possible ...

==21620== 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!

~/workspace/pset5/speller/ $ check50 cs50/2018/x/speller Connecting..... Authenticating....... Preparing................ Uploading............. Checking........ :) dictionary.c, dictionary.h, and Makefile exist :) speller compiles :) handles most basic words properly :) handles min length (1-char) words :) handles max length (45-char) words :) handles words with apostrophes properly :) spell-checking is case-insensitive :) handles substrings properly :( program is free of memory errors valgrind tests failed; rerun check50 with --log added to end of command for more information. See https://cs50.me/checks/6c42ab975ef6473130e4f48c1e5e74a5910dffd7 for more detail. :( program is free of memory errors valgrind tests failed; rerun check50 with --log added to end of command for more information.

Log running valgrind ./speller substring/dict substring/text... checking for output "MISSPELLED WORDS ca cats caterpill caterpillars WORDS MISSPELLED: 4 WORDS IN DICTIONARY: 2 WORDS IN TEXT: 6 "... checking that program exited with status 0... checking for valgrind errors... Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 33) Use of uninitialised value of size 8: (file: dictionary.c, line: 33) Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 60)

bool check(const char *word)
{

// TODO
int j=0; // for children[j]
int k; // for strlen(word)
node *travs=root;
 for (k=0;k <= strlen(word) +1;k++)
   {
    if(isalpha(word[k]) || word[k]=='\'')               this is line:33
                     {
                      j=tolower(word[k])-'a';
                         if(j==-58)
                                {
                                 j=26;
                                 }

   if (travs->children[j]==NULL)
     {
        k=0;
        travs=root;
        return false;


     }

  else if (travs->children[j]!=NULL)
    {

        travs=travs->children[j];
    }

   } //end of if(isalpha(letter) || letter=='\'' )

   if(word[k]=='\0')                                     -- this is line: 60
    {

         if (travs->is_word==1)
             {
                 return true;
                 k=0;
            }
     }





bool load(const char *dictionary)
{
 root = calloc(1, sizeof(node));
  FILE *inptr=fopen(dictionary,"r");
   int i=0; //for children[i]
  if(inptr==NULL)
  {
  printf("Error opening file");
  return 0;
  }
   char letter='\0';
   node *ptr=root;
   do
  {
    letter=fgetc(inptr);
   if (letter=='\n')
   {
       ptr-> is_word=true;
       wordcount++;
       ptr=root;
   }
   if(isalpha(letter) || letter=='\'' )
  {
   i = tolower(letter) - 'a';
    if (i==-58) //ASCII code of '\'' - 'a'
    {
     i=26;
     }

   //check children[i]’s value, if the value in children[i] is NULL, calloc a new node, then let children[i] point to it
   if(ptr->children[i]==NULL)
   {
     ptr->children[i] = calloc(1, sizeof(node));
     nodecount++;
     ptr=ptr->children[i];

    }

     else if (ptr->children[i]!=NULL)
    {
      ptr=ptr->children[i];

    }

  } // end of if(isalpha(letter) || letter=='\'' )

  } // end of do

  while (letter != EOF);

  fclose(inptr);
   return true;
} // end of bool load(const char *dictionary)
2
  • There's likely a problem in load.
    – Blauelf
    Commented Jun 3, 2019 at 15:44
  • Hi, i add the LOAD at above. Could you pls help me to indicate the problem? Thank you very much.
    – user21342
    Commented Jun 4, 2019 at 8:26

1 Answer 1

1

That one was tricky, I initially missed it completely. In check, you write

 for (k=0;k <= strlen(word) +1;k++)

Both, the +1, and using <= instead of <, would individually allow you to loop strlen(word)+1 times. Together, you have it loop one too far. So the easy solution would be to change one of the two.

My preferred solution would be to use neither <= nor +1, and instead of inside some if(word[k]=='\0'), write my return travs->is_word; after the loop.

1
  • Thank you sooooo~ much~~~~~!!!!
    – user21342
    Commented Jun 4, 2019 at 14:04

You must log in to answer this question.

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