0

can someone help me please figure out what am i doing wrong with imlementing the load function: 1.when i try to run the program i get a "killed" massage. 2. it looks like i have a leak of memo but i cant find where here is my code:

/** * Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) {

struct Node *root;
root=malloc(sizeof(Node));
for (int i=0;i<26;i++)
{
    root->children[i]=NULL;
}
Node *ptr = root;

//open dictionart file
FILE* dp = fopen(dictionary, "r");
if (dp == NULL)
{
    printf("Could not open %s.\n", dictionary);
    unload();
    return 1;
} 
//read words from dictinary
char c;
while((c=fgetc(dp))!=EOF)
{
    while (c !='\0')
    {

        int index = (tolower(c) - 'a'); 
        ptr->is_word=false;
        if (ptr->children[index]==NULL)
        {
        struct Node *next;
        next=malloc(sizeof(Node));
        ptr->children[index]=next;
        }   
        else
        ptr=ptr->children[index];
    }
    ptr->is_word=true;
}

return false;

}

0

One potensial error is that you should think about if char c is the correct holder of a potensial 47 char length word...

" //read words from dictinary char c;"

1
  • Thanks for the hint. I tried to implement it as a char because i thought there is no point in reading it as separated words. for each char it can be one of three options: 1. a part of a non-existing word 2. a part of an already exist word 3. the end of a word. but now I see that the second loop is infinite... – Oicarmel Oct 24 '16 at 17:48

You must log in to answer this question.

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