0

Hi could someone have a look on my dictionary.c pls and tell me why am I getting so many of unitialized errors when I have initialized pointers to NULL I don't understand why is valgrind telling me I didn't. #include #include #include #include #include

#include "dictionary.h"
#include "usefull.h"
tries* newL = NULL;
tries* root = NULL;

/**
 * Returns true if word is in dictionary else false.
 */
  bool check(const char* word)
   {
     newL = root;
     for(int c = 0, d = strlen(word); c < d ;c++)
   {

     //newL = root;
      int index;

    // printf("%c",word[c]); 

      if(isalpha(word[c]))
      {
          index = word[c] - 97 ;
         // printf("%d\n\n",index);  
      }
      if(word[c] == 39)
      {
           index = 26;
      }
       //printf("\n%i\n",(char) newL->children[0]);
      if(newL->children[index] == NULL)
      { 
            return false;
       }
      else if(newL->children[index] != NULL)
      {
          newL = newL->children[index];
       //printf("letter found in word");
       }
       else if(c == d - 1)
       {
            if(newL->end_Word)
       {
            return true;
       }
       else if(!newL -> end_Word)
       {
            return false;
       }
    }



    }
         return true;
    }


     // Loads dictionary into memory.  Returns true if successful else false.
     bool load(const char* dictionary)
     {
       s = malloc(sizeof(int)); 
      //open file for reading
      FILE *dict = fopen(dictionary,"r");
      if(dict == NULL)
   {
      printf("Could not open file!!");
      unload();
      return false;
   }
   int letterIndex = 0;

   root = malloc(sizeof(tries));
   newL = root;
  //go throught every single character of every single word in the dictionary
   for(int c = fgetc(dict); c != EOF; c = fgetc(dict))
   {
//setting Index
   letterIndex = -1; 

    //check if the character is valid

        //set the letter index
 //setting index for ' char
    if (c == 39)
    {
       letterIndex = 26;
    }
//getting index of alphanumeric char
    else if (isalpha(c))
    {
         letterIndex = c - 97;

    }

    //allocate memory for every node

    //if index not changed , new word
    if(letterIndex == -1)
    {
        *s += 1;
        newL -> end_Word = true; 
        newL = root;
    }
//if letter found
//if pointing to NULL
    else if(newL->children[letterIndex] == NULL)
    {
        newL-> children[letterIndex] = malloc(sizeof(tries));
        newL = newL->children[letterIndex];
    }
//if already pointing 
    else 
    {
        newL = newL -> children[letterIndex];
    }
}
    return true;
}




/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
 unsigned int size(void)
 {


 return *s;
 }

/**
* Unloads dictionary from memory.  Returns true if successful else false.
*/
            void tUnload(tries* try)
            {
   //traverse through all of the children
   for (int i = 0; i < 27; i++)
   { 

     //if child == value, recursively get to the bottom
     if(try->children[i] != NULL)             {
        tUnload(try->children[i]);

     }
     //free up the parent
     free(try);
   }


}



 bool unload(void)
 {

     tUnload(newL);
     return true;
  }
               Memcheck, a memory error detector
               Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
               Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
               Command: ./speller dictionaries/small text.txt

               Conditional jump or move depends on uninitialised value(s)
                  at 0x4012FF: load (dictionary.c:120)
                  by 0x4008BD: main (speller.c:50)

MISSPELLED WORDS

               Conditional jump or move depends on uninitialised value(s)
                  at 0x4010CB: check (dictionary.c:44)
                  by 0x400C47: main (speller.c:122)

abcd Conditional jump or move depends on uninitialised value(s) at 0x401421: free_memory (dictionary.c:155) by 0x401438: free_memory (dictionary.c:157) by 0x401438: free_memory (dictionary.c:157) by 0x401438: free_memory (dictionary.c:157) by 0x401438: free_memory (dictionary.c:157) by 0x401438: free_memory (dictionary.c:157) by 0x401438: free_memory (dictionary.c:157) by 0x401438: free_memory (dictionary.c:157) by 0x401438: free_memory (dictionary.c:157) by 0x4013D0: unload (dictionary.c:175) by 0x400DCC: main (speller.c:162)

               Conditional jump or move depends on uninitialised value(s)
                  at 0x401421: free_memory (dictionary.c:155)
                  by 0x401438: free_memory (dictionary.c:157)
                  by 0x401438: free_memory (dictionary.c:157)
                  by 0x4013D0: unload (dictionary.c:175)
                  by 0x400DCC: main (speller.c:162)

               Conditional jump or move depends on uninitialised value(s)
                  at 0x401421: free_memory (dictionary.c:155)
                  by 0x401438: free_memory (dictionary.c:157)
                  by 0x4013D0: unload (dictionary.c:175)
                  by 0x400DCC: main (speller.c:162)

               Conditional jump or move depends on uninitialised value(s)
                  at 0x401421: free_memory (dictionary.c:155)
                  by 0x4013D0: unload (dictionary.c:175)
                  by 0x400DCC: main (speller.c:162)

WORDS MISSPELLED: 1 Conditional jump or move depends on uninitialised value(s) at 0x4E814CE: vfprintf (vfprintf.c:1660) by 0x4E8B3D8: printf (printf.c:33) by 0x400E6E: main (speller.c:177)

               Use of uninitialised value of size 8
                  at 0x4E8099B: _itoa_word (_itoa.c:179)
                  by 0x4E84636: vfprintf (vfprintf.c:1660)
                  by 0x4E8B3D8: printf (printf.c:33)
                  by 0x400E6E: main (speller.c:177)

               Conditional jump or move depends on uninitialised value(s)
                  at 0x4E809A5: _itoa_word (_itoa.c:179)
                  by 0x4E84636: vfprintf (vfprintf.c:1660)
                  by 0x4E8B3D8: printf (printf.c:33)
                  by 0x400E6E: main (speller.c:177)

               Conditional jump or move depends on uninitialised value(s)
                  at 0x4E84682: vfprintf (vfprintf.c:1660)
                  by 0x4E8B3D8: printf (printf.c:33)
                  by 0x400E6E: main (speller.c:177)

               Conditional jump or move depends on uninitialised value(s)
                  at 0x4E81599: vfprintf (vfprintf.c:1660)
                  by 0x4E8B3D8: printf (printf.c:33)
                  by 0x400E6E: main (speller.c:177)

               Conditional jump or move depends on uninitialised value(s)
                  at 0x4E8161C: vfprintf (vfprintf.c:1660)
                  by 0x4E8B3D8: printf (printf.c:33)
                  by 0x400E6E: main (speller.c:177)

WORDS IN DICTIONARY: 7 WORDS IN TEXT: 1 TIME IN load: 0.02 TIME IN check: 0.00 TIME IN size: 0.00 TIME IN unload: 0.00 TIME IN TOTAL: 0.02

               Invalid read of size 4
                  at 0x4EB2F2D: _IO_flush_all_lockp (genops.c:841)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4E7315A: __run_exit_handlers (exit.c:95)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc150 is 192 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB3020: _IO_flush_all_lockp (genops.c:841)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4E7315A: __run_exit_handlers (exit.c:95)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0b0 is 32 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB3024: _IO_flush_all_lockp (genops.c:841)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4E7315A: __run_exit_handlers (exit.c:95)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0b8 is 40 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB2FCA: _IO_flush_all_lockp (genops.c:862)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4E7315A: __run_exit_handlers (exit.c:95)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0f8 is 104 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 4
                  at 0x4EB30D0: _IO_unbuffer_write (genops.c:952)
                  by 0x4EB30D0: _IO_cleanup (genops.c:1022)
                  by 0x4E7315A: __run_exit_handlers (exit.c:95)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc090 is 0 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid write of size 4
                  at 0x4EB3170: _IO_unbuffer_write (genops.c:990)
                  by 0x4EB3170: _IO_cleanup (genops.c:1022)
                  by 0x4E7315A: __run_exit_handlers (exit.c:95)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc150 is 192 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB317A: _IO_unbuffer_write (genops.c:950)
                  by 0x4EB317A: _IO_cleanup (genops.c:1022)
                  by 0x4E7315A: __run_exit_handlers (exit.c:95)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0f8 is 104 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 4
                  at 0x4EB2F2D: _IO_flush_all_lockp (genops.c:841)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4F9C17A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
                  by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
                  by 0x4E7316A: __run_exit_handlers (exit.c:97)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc150 is 192 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB3020: _IO_flush_all_lockp (genops.c:841)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4F9C17A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
                  by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
                  by 0x4E7316A: __run_exit_handlers (exit.c:97)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0b0 is 32 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB3024: _IO_flush_all_lockp (genops.c:841)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4F9C17A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
                  by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
                  by 0x4E7316A: __run_exit_handlers (exit.c:97)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0b8 is 40 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB2FCA: _IO_flush_all_lockp (genops.c:862)
                  by 0x4EB30B9: _IO_cleanup (genops.c:1013)
                  by 0x4F9C17A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
                  by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
                  by 0x4E7316A: __run_exit_handlers (exit.c:97)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0f8 is 104 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 4
                  at 0x4EB30D0: _IO_unbuffer_write (genops.c:952)
                  by 0x4EB30D0: _IO_cleanup (genops.c:1022)
                  by 0x4F9C17A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
                  by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
                  by 0x4E7316A: __run_exit_handlers (exit.c:97)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc090 is 0 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid write of size 4
                  at 0x4EB3170: _IO_unbuffer_write (genops.c:990)
                  by 0x4EB3170: _IO_cleanup (genops.c:1022)
         by 0x4F9C17A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
                  by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
                  by 0x4E7316A: __run_exit_handlers (exit.c:97)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc150 is 192 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)

               Invalid read of size 8
                  at 0x4EB317A: _IO_unbuffer_write (genops.c:950)
                  by 0x4EB317A: _IO_cleanup (genops.c:1022)
                  by 0x4F9C17A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
                  by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
                  by 0x4E7316A: __run_exit_handlers (exit.c:97)
                  by 0x4E731F4: exit (exit.c:104)
                  by 0x4E58F4B: (below main) (libc-start.c:321)
                Address 0x51fc0f8 is 104 bytes inside a block of size 568 free'd
                  at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
                  by 0x40137F: load (dictionary.c:131)
                  by 0x4008BD: main (speller.c:50)


               HEAP SUMMARY:
                   in use at exit: 0 bytes in 0 blocks
                 total heap usage: 26 allocs, 26 frees, 6,292 bytes allocated

               All heap blocks were freed -- no leaks are possible

               For counts of detected and suppressed errors, rerun with: -v
               Use --track-origins=yes to see where unitialized values
               come from.
               ERROR SUMMARY: 642 errors from 26 contexts (suppressed: 0 from 0)

1 Answer 1

1

If this code encounters a single null child, it will delete the parent. Any non-null children that still exist will become lost memory. The code needs to traverse ALL of the children and make sure they are all null or recurse into the non-null children and process them. Once all the children have been traversed, then the parent can be freed and the function can return to the calling recursion.

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

7
  • Thank you for response. I am trying to figure out how to code it. If I manage to do it I will tick this as finished :)
    – Kamil Kug
    Commented Sep 5, 2016 at 14:40
  • I have updated my code but still can not get it to work, I have tried to just check all of the elements whether they are NULL or not and if not than traverse into every single element until the bottom is reached using the recursive way and than removing the parent. For some reason I still can not get it to work:( Any idea why it might not be working still although I have updated the code ? I must have messed up somewhere.
    – Kamil Kug
    Commented Sep 5, 2016 at 16:05
  • 1
    You incremented i inside the if statement. This is causing you to skip over nodes.
    – Cliff B
    Commented Sep 5, 2016 at 19:50
  • I had done it as I was thinking if the node had a zaro value I needed to check another one and I thought that I didn't have to free that one up so I just simply skipped it. I will look into that again , thanks for your advice.
    – Kamil Kug
    Commented Sep 5, 2016 at 19:55
  • 1
    Doing nothing will skip it. It would be simpler to just change the whole if/else to an if statement and test != NULL and drop the else clause.
    – Cliff B
    Commented Sep 5, 2016 at 20:05

You must log in to answer this question.

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