0

Unload function giving segmentation fault. Would anyone help me check my entire code.

#include <stdbool.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

#include "dictionary.h"

        int number = 0;
        bool check(const char *word)
        {
        wordtree* trav = head;
        char checking[LENGTH+1];
        strcpy(checking,word);
        for(int i=0,n=strlen(word);i<n;i++)
            {
                if(word[i] == '\'')
                    {   
                        if(trav->ptr[26]!=NULL)
                        {
                        trav = trav->ptr[26];
                        }
                        else
                        return false;
                    }
                else
                {
                    int key  = tolower(word[i]) - 97;
                    if(trav->ptr[key]!=NULL)
                    {
                    trav = trav->ptr[key];
                    }
                    else
                    return false;
                }
            }
        for(int i=0,n=strlen(checking);i<n;i++)
            {
                checking[i] = tolower(checking[i]);

            }
        if(strcmp(checking,trav->cha)==0)
            {
                return true;
            }
        return false;
    }


    bool load(const char* dictionary)
    {
    head = malloc(sizeof(wordtree));
    char word[LENGTH+1];
    int index = 0;
    FILE* dict = fopen(dictionary,"r");
    if(dict == NULL)
     {
        printf("could not load dictionary\n");
        return false;
     }

    wordtree* trav = head;
    for(char c = fgetc(dict); c!=EOF; c = fgetc(dict))
        {
         if(c!='\n')
         {
            word[index] = c;
            index++;
            if(c=='\'')
            {   if(trav->ptr[26]==NULL)
                {
                wordtree* root = malloc(sizeof(wordtree));
                trav->ptr[26] = root;

                }
                trav = trav->ptr[26];

            }
            else
            {
                c = toupper(c);
                int key = c - 65;
                if(trav->ptr[key]==NULL)
                {
                wordtree* root = malloc(sizeof(wordtree));
                trav->ptr[key] = root;

                }
                 trav=trav->ptr[key];


            }
         }
         else if(c=='\n')
         {
            word[index]='\0';
            index =0;
            for(int i=0,n=strlen(word);i<n;i++)
            {
                trav->cha[i] = word[i];
            }
            number++;
            trav = head;

         }
        }

        return true;
       }


        unsigned int size(void)
         {

           return number;
          }


         void triefree(wordtree* del);
         bool unload()
         {
        triefree(head);  
        return true;
         }

         void triefree(wordtree* del)
          {

          for(int i=0;i<27;i++)
          {
        if(del->ptr[i]!=NULL)
        {
            del = del->ptr[i];
            triefree(del);
        }
    }
    free(del);


}
2
  • If you are getting a seg fault in unload, that means that you are successfully compiling your code. Unfortunately, the code above doesn't compile, even after making educated guesses to what was added to dictionary.h. It is likely that you are compiling a later version of the code than the one that is posted (or the IDE is acting up again and isn't actually saving later changes.) Please confirm that your code does successfully compile and then edit the question to post both the current code that you're working with and the additions to the .h file so that they can be tested.
    – Cliff B
    Dec 11 '16 at 6:52
  • Also, while the purpose of this forum is to both teach and to assist with finding specific problems, full code reviews are outside the boundaries of what is appropriate here. If you have other specific questions, please feel free to post a new specific question.
    – Cliff B
    Dec 11 '16 at 6:54

You must log in to answer this question.

Browse other questions tagged .