0

the thing loads fine but it has problems along the line so i run help50 and it gives me the following feedback:

==8299== 
==8299== HEAP SUMMARY:
==8299==     in use at exit: 308,079,072 bytes in 1,375,353 blocks
==8299==   total heap usage: 1,375,370 allocs, 18 frees, 308,083,568 bytes allocated
==8299== 
==8299== 45,472 bytes in 203 blocks are possibly lost in loss record 1 of 3
==8299==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8299==    by 0x40101B: gnew_node (dictionary.c:30)
==8299==    by 0x40120C: load (dictionary.c:75)
==8299==    by 0x4008C4: main (speller.c:40)
==8299== 
==8299== 308,033,600 (7,168 direct, 308,026,432 indirect) bytes in 32 blocks are definitely lost in loss record 3 of 3
==8299==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8299==    by 0x40101B: gnew_node (dictionary.c:30)
==8299==    by 0x40120C: load (dictionary.c:75)
==8299==    by 0x4008C4: main (speller.c:40)
==8299== 
==8299== LEAK SUMMARY:
==8299==    definitely lost: 7,168 bytes in 32 blocks
==8299==    indirectly lost: 308,026,432 bytes in 1,375,118 blocks
==8299==      possibly lost: 45,472 bytes in 203 blocks
==8299==    still reachable: 0 bytes in 0 blocks
==8299==         suppressed: 0 bytes in 0 blocks
==8299== 
==8299== For counts of detected and suppressed errors, rerun with: -v
==8299== ERROR SUMMARY: 60 errors from 8 contexts (suppressed: 0 from 0)

Helping with...                                         

==8299== Invalid read of size 8
==8299==    at 0x4011FC: load (dictionary.c:72)
==8299==    by 0x4008C4: main (speller.c:40)

Looks like you're trying to access 8 bytes of memory
that isn't yours? Did you try to index into an array
beyond its bounds? Take a closer look at line 72 of
dictionary.c.

i dont understand why and how am wrong, please help me with ideas. the following is my code:

/**
 * Implements a dictionary's functionality.
 */
    #include<string.h>
    #include <stdbool.h>
    #include<stdlib.h>
    #include "dictionary.h"
    #include<stdio.h>
    #include<ctype.h>

    typedef struct node
    {
    bool is_word;
    struct node*next_char[27];
    }node;
    struct node*root_ptr=NULL;
    //make new node and initialize to NULLs
    struct node*gnew_node(void)
     {
      struct node*children=NULL;
      children=(node*)malloc(sizeof(node));
      if(children==NULL)
      {
       unload();
       return false;
      }
      children->is_word=false;
      for(int i=0;i<=26;i++)
      {
       children->next_char[i]=NULL;
      }
      return children;
     }
    /**
     * Loads dictionary into memory. Returns true if successful else false.
     */
    bool load(const char *dictionary)
    {
    // TODO
    root_ptr=(node*)malloc(sizeof(node));
    node *trav=root_ptr;
    FILE *fp=fopen(dictionary,"r");
    if(fp==NULL)
    {
        printf("dictionary failled to load\n");
        unload();
        return false;
    }

    //scan dictionary word by word
    char c;
    while((c=fgetc(fp))!=EOF)
    {
        if(c=='\n')
           {
            trav->is_word=true;
            trav=root_ptr;

           }
        else
        {
         int INDEX=(tolower(c))-'a';
         if(trav->next_char[INDEX]==NULL)
            {

             trav->next_char[INDEX]=gnew_node();
            }
             trav=trav->next_char[INDEX];
        }

     }

      fclose(fp);
      return true;
    }

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

    return false;
    }

    /**
     * Unloads dictionary from memory. Returns true if successful else false.
     */
    bool unload(void)
      {
    // TODO
    struct node* Pnode=root_ptr;

    for(int k=26;k>=0;k--)
      {
       if(Pnode->next_char[k]!=NULL)
       {
       free(Pnode->next_char[k]);
       }
      }

        free(root_ptr);

        return true;
    }
0

You've jumped too far ahead in trying to resolve valgrind issues. There are a number of issues that need to be fixed.

The code is marking the end of words based on an apostrophe. Apostrophes must be treated like any other letter. The end of each word from the dictionary is the linefeed character \n. This will require a redesign to treat words correctly.

The code posted above doesn't compile. First, root_ptr isn't declared anywhere. I'm assuming that it is a global that is probably declared in the .h file. You should do it in the dictionary.c file. check50 only uploads the dictionary.c file to check, so changes anywhere else won't be processed. Second, the following line is a problem:

    free(gnew_node);

A function can't be freed.

There's not much that can be said beyond this. There's a fair amount of redesigning and recoding that needs to be done. Happy coding! ;-)

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

1
  • thank you Cliff.I made the changes on \n root_ptr and gnew_node.the thing compiles fine except when i run help50, i get the same error message.I think there are some data lost along the process and according to the error message it brought by gnew_node and when i use it in load. also how do i free the malloc on gnew because its bringing error on unload so i removed free(gnew_node) – TabithaWamucii Nov 30 '17 at 12:37

You must log in to answer this question.

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