0

can anyone have a look at my code, find the problem and suggest improvements.

im getting segmentation fault after calling 'check' function.

typedef struct trie
{
  bool word;
  struct trie* link[27];

 }trie;

 trie* root=NULL;
 int number=0;   


/*check function*/

bool check( const char* word)
{
     int x=0;
     char str[46];
     trie* temp=root;
     strcpy(str,word);

     int i=0;

     while(str[i]!='\0')
     {
         if(isalpha(str[i]) )
         {
             tolower(str[i]);
             x=str[i]-'a';

         }

         else if(str[i]=='\'') x=26;

         else return false;

         if(temp->link[x]==NULL)
         return false;



             temp=temp->link[x];



         i++;

     }



 if(temp->word==true)
 return true;
 else 
 return false;

}

 //Load function 

bool load(const char* dictionary)
{


   FILE*fp=fopen(dictionary,"r");
   root=calloc(1,sizeof(trie));
   if(fp==NULL)
  {
       return false;
   }

   trie*temp;
   char str[46];
   int i,x;

   while(fscanf(fp,"%s",str)!=EOF)
   {
        i=0;
        temp=root;

        while(str[i]!='\0')
        {
            if(isalpha(str[i]))
             x=tolower(str[i])-'a';
            else if(str[i]=='\'')
             x=26;
            else
             return false;

            if(temp->link[x]==NULL)
             {

                 temp->link[x]=calloc(1,sizeof(trie));

             }

             temp=temp->link[x];
             temp->word=false;
             if(str[i+1]=='\0')
              temp->word=true;

              i++;   


        }
        number++;
    }
   fclose(fp);
   return true;

} 

valgrind::

 ==15667== Command: ./speller texts/austinpowers.txt
 ==15667== 

 MISSPELLED WORDS

 ==15667== Invalid read of size 8
 ==15667==    at 0x40110C: check (dictionary.c:46)
 ==15667==    by 0x400CB7: main (speller.c:119)
 ==15667==  Address 0x51fc1c8 is 392 bytes inside a block of size 568   free'd
 ==15667==    at 0x4C2BDEC: free (in  /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==15667==    by 0x4EA4AE4: fclose@@GLIBC_2.2.5 (iofclose.c:85)
 ==15667==    by 0x40139C: load (dictionary.c:132)
 ==15667==    by 0x40092D: main (speller.c:45)
 ==15667== 
 ==15667== Invalid read of size 8
 ==15667==    at 0x401183: check (dictionary.c:65)
 ==15667==    by 0x400CB7: main (speller.c:119)
 ==15667==  Address 0x51fc270 is 560 bytes inside a block of size 568  free'd
 ==15667==    at 0x4C2BDEC: free (in  /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==15667==    by 0x4EA4AE4: fclose@@GLIBC_2.2.5 (iofclose.c:85)
 ==15667==    by 0x40139C: load (dictionary.c:132)
 ==15667==    by 0x40092D: main (speller.c:45)
 ==15667== 
 ==15667== 
 ==15667== Process terminating with default action of signal 11  (SIGSEGV)
 ==15667==  General Protection Fault
 ==15667==    at 0x40110C: check (dictionary.c:46)
 ==15667==    by 0x400CB7: main (speller.c:119)
 ==15667== 
 ==15667== HEAP SUMMARY:
 ==15667==     in use at exit: 82,226,936 bytes in 367,083 blocks
 ==15667==   total heap usage: 367,084 allocs, 1 frees, 82,227,504 bytes allocated
 ==15667== 
 ==15667== LEAK SUMMARY:
 ==15667==    definitely lost: 0 bytes in 0 blocks
 ==15667==    indirectly lost: 0 bytes in 0 blocks
 ==15667==      possibly lost: 0 bytes in 0 blocks
 ==15667==    still reachable: 82,226,936 bytes in 367,083 blocks
 ==15667==         suppressed: 0 bytes in 0 blocks
 ==15667== Reachable blocks (those to which a pointer was found) are not shown.
 ==15667== To see them, rerun with: --leak-check=full --show-leak-kinds=all
 ==15667== 
 ==15667== For counts of detected and suppressed errors, rerun with: -v
 ==15667== ERROR SUMMARY: 32 errors from 2 contexts (suppressed: 0 from 0)
 Segmentation fault

1 Answer 1

0

strong texta fellow redditor helped me ..thanks Grithga ;0)

Compare how you use tolower in load (which is correct):

    x=tolower(str[i])-'a';

With how you use it in check (which is incorrect):

   tolower(str[i]);

   x=str[i]-'a';

Additionally, you do have a problem in load that could cause you to "remove" previously added words by marking them as not being a word.

You must log in to answer this question.

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