0

So I finished speller and am debugging and getting a segmentaiton error when I am trying to set the array in my root node to NULL.

I have defined my Struct at the beginning of Dictionary.c and declared root there too :-

typedef struct node
    {
        bool endOfWord;
        struct node* child[27];
    }
    node;

    node* root;

Then in load I iterate over roots array setting them to NULL:-

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

and that is where GDB is telling me I have a problem:-

Program received signal SIGSEGV, Segmentation fault.
0x08049004 in load (dictionary=0x80495c2 "/home/cs50/pset5/dictionaries/large") at dictionary.c:74
74          root -> child[i] = NULL;

Not sure what I'm doing wrong. Should I be using malloc to allocate memory to root?

1 Answer 1

3

It seems to me that the problem is that you did not initialize root, you just declared as a pointer and then without initializing it you are trying to set the values to NULL.

You need to initialize root before setting its values to NULL, you can do this with malloc():

root = malloc(sizeof(node));
1
  • yup that was it.thx
    – Gordon
    Commented Aug 11, 2015 at 11:02

You must log in to answer this question.

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