1

I cannot figure what's wrong with the code, it gives me a segfault, but runs in valgrind and in gdb. So far, i only wrote the load function, so anything else is still empty.

    #include <stdbool.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "dictionary.h"
    typedef struct node
    {
    bool is_word;
    struct node* children[27];
    }
    node;
    int size_dict;
    bool load(const char* dictionary)
    {
    // TODO
    node* root = NULL;
node* new = NULL;
node* curr = NULL;
FILE* dict = fopen(dictionary, "r");
if (dict == NULL)
{
    printf("Could not open %s.\n", dictionary);
    unload();
    return 1;
}
int index = 0;
char entry[LENGTH + 1];
char c = ' ';
size_dict = 0;
int idx_ltr = 0;
root = malloc(sizeof(node));
for(int i = 0; i < 27; i++)
{
    root -> children[i] = NULL;
}
do
{
    fread(&c, sizeof(char), 1, dict);
    while(c != '\n')
    {
        entry[index] = c;
        index++;
        fread(&c, sizeof(char), 1, dict);
    }
    entry[index] = '\0';
    curr = root;
    int counter = 0;
    do
    {
        if(entry[counter] == '\'')
        {
            idx_ltr = 26;
        }
        else
        {
            idx_ltr = entry[counter] - 'a';
        }
        if(counter + 1 == index)
        {
            if (curr -> children[idx_ltr] == NULL)
            {
                curr -> children[idx_ltr] = curr;
                curr -> is_word = true;
                break;
            }
        }
        else
        {   
            new = malloc(sizeof(node));
            for(int j = 0; j < 27; j++)
            {
                new -> children[j] = NULL;
            }
         curr -> children[idx_ltr] = new;
         curr = new;
         counter++;
         }   
    }while(counter < index);
    size_dict++;
    index = 0;
}while(!feof(dict));
fclose(dict);
printf("%i\n", size_dict);
return true;
}

Still, i think it's also buggy when building the trie, since when running valgrind i get this definitely lost blocks, and using small dictionary i should have only 11 mallocs (1 for root + 2 for cat and + 8 erpillar):

    ==10751== 
    ==10751== HEAP SUMMARY:
    ==10751==     in use at exit: 1,568 bytes in 14 blocks
    ==10751==   total heap usage: 16 allocs, 2 frees, 2,272 bytes allocated
    ==10751== 
    ==10751== Searching for pointers to 14 not-freed blocks
    ==10751== Checked 57,456 bytes
    ==10751== 
    ==10751== LEAK SUMMARY:
    ==10751==    definitely lost: 336 bytes in 3 blocks
    ==10751==    indirectly lost: 1,232 bytes in 11 blocks
    ==10751==      possibly lost: 0 bytes in 0 blocks
    ==10751==    still reachable: 0 bytes in 0 blocks
    ==10751==         suppressed: 0 bytes in 0 blocks
    ==10751== Rerun with --leak-check=full to see details of leaked memory
    ==10751== 
    ==10751== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    ==10751== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Sorry, but i'm desperate cause the course ends in the 31th, and i got a license from work to do it,so i must manage to end it (all others pset's are done). Thank you guys.

0

The seg fault occurs because you aren't handling the EOF of the dictionary file correctly. The EOF won't be detectable until you execute the first read after the last character in the file. That read will generate the EOF, but you then try to process what is "read" as a character. The EOF detection is at the very bottom of the while loop.

You should consider restructuring your code. Instead of do / while( !EOF ), use while(fread()==...) {...} so that it detects EOF immediately instead of trying to process more code. (I'll leave it to you to figure out exactly how to code it.)

Another freebie hint - you're going to have a problem downstream because you've declared root as a local variable inside of load().

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

2
  • I've managed the segfault's, changing the do-while structure, but not in the way you answered me (because i was trying it all day long). The hint was perfect, since valgrind now don't show any loss. Still, i'm not sure if i built the trie correctly, because using small dictionary it still mallocs 14 times. Should i post another question with the current code, or is there another way to continue questioning right under this one? – user10187 Dec 9 '15 at 21:03
  • new problem, new question. ;-) – Cliff B Dec 9 '15 at 21:28

You must log in to answer this question.

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