0

I'm not sure I am using calloc correctly as it is my first time attempting after getting random seg faults using malloc. Anyway, after changing my code to use calloc instead of malloc, I am still getting seg fault but in different place. Here is my load code:

{
FILE* infile = fopen(dictionary, "r");
if (infile == NULL)
{
    return false;
    fclose(infile);
}

node* root = calloc(1, sizeof(node));
node* new_node = root;

if(root == NULL)
{
    return 1;
}

for(int i = 0; i < 27; i++)
{
    root -> children[i] = NULL;
    root -> is_word = false;
}

{
    int count = 0, words = 0;

    for (int c = fgetc(infile); c != EOF; c = fgetc(infile))
    {
        if (c == '\n')
        {
            count++;
        }
        if (isalpha(c))
        {
            c = ('c' - 'a');

            if (new_node -> children[c] == NULL)
            {
                new_node -> children[c] = calloc(1, sizeof(new_node));
                new_node = new_node -> children[c];
            }
            else
            {
                new_node = new_node -> children[c];
            }
        }
        else if (c == '\'')
        {
            c = 26;

            if (new_node -> children[c] == NULL)
            {
                new_node -> children[c] = calloc(1, sizeof(new_node));
                new_node = new_node -> children[c];
            }
            else
            {
                new_node = new_node -> children[c];
            }
        }

        if (count > 0)
        {
            new_node -> is_word = true;
            new_node = root;
            words++;
            count = 0;
        }
    }
}
fclose(infile);
return true;
}

When run in GDB, I am getting a seg fault when it hits line 93 - if (new_node -> children[c] == NULL) I thought these kinds of seg faults would go away when using calloc. Can anyone explain why it seg faults when looking at my code?

1 Answer 1

0

It might be sizeof here new_node -> children[c] = calloc(1, sizeof(new_node));. new_node is a pointer, sizeof a pointer is like 8. Probably a typo; look at the allocation of root earlier in the code.

Speaking of the allocation of root....It is declared locally here
node* root = calloc(1, sizeof(node));, thus creating a shadow variable of anything declared in, say, dictionary.h.

Another problem is here c = ('c' - 'a');. c is always 2.

Hopefully this gets a leg up on the seg fault so you can move on to still yet even more exciting troubleshooting adventures :)

2
  • Thank you so much! Was totally overlooking both typos. I have a question about defining the node and about how programs compile in general. When you compile speller.c the makefile includes dictionary.h. I was under the impression that if I defined node and/or root in dictionary.h, then it would act globally. Is this not so? Commented Dec 22, 2016 at 4:31
  • Speaking of typos.....my fingers said "allocated" when they should have said "declared" :). The answer is emended. You are correct. Commented Dec 22, 2016 at 12:23

You must log in to answer this question.

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