-1

I'm starting to work with tries in C and I made this really simple program, but somehow it crashes. I don't know why but if you could take a look at my code and tell me that would be great.

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>

typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

node* root;

int main()
{
    root->children[0]=NULL;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

2

Before using a pointer you have to initialize it. In this particular example, you will most likely get a NULL pointer since according to the C standard global variables should be initialized to 0.

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>

typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

node* root;

int main()
{
    root = malloc(sizeof(node));
    root->children[0]=NULL;
    free(root);
}
bottaio
  • 4,963
  • 3
  • 19
  • 43
2

The problem lies here.

node *root;

You have declared root but you have not defined it. By default this contains some garbage address or being global a NULL, which is illegal to access and hence the program crashes.

Initialise your root to the first node of the trie and then use it further.

Doing this operation-

root->children[0] = NULL

translates to someGarbageValue/NULL -> children[0] = NULL //makes no sense. Initialise your root to your first node.

For initialising the root you can allocate memory to it.

root = (node *)malloc(sizeof(node));

this will allocate the memory from the heap, and malloc will store the starting address of that allocated memory into the root node pointer.

Don't forget to free this memory to avoid memory leaks when you are done with it.

free(root)
Dark Innocence
  • 1,389
  • 9
  • 17
  • But how does one initialize the root? – Carey Gregory Aug 27 '16 at 20:24
  • you can malloc your root. I'll post it my answer. – Dark Innocence Aug 27 '16 at 20:27
  • Updated the answer. – Dark Innocence Aug 27 '16 at 20:30
  • Better but... http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Carey Gregory Aug 27 '16 at 20:36
  • It's preference for me. I don't go by that answer. Usually when debugging long codes, I won't scroll up and see what was the type of the root or some other entity. It will be written infront of it. Always typecast your malloc to follow your organisation coding guidelines and most of them have included this one as well :) – Dark Innocence Aug 27 '16 at 20:39
  • Another reason I can come with is portability, i.e portable between different versions of C, if you have some legacy code. – Dark Innocence Aug 27 '16 at 20:41
  • The legacy code issue is largely imaginary unless you're using compilers that predate C89, which would make them extremely old compilers compliant with no standards. The reasons why casting `malloc` is a bad idea are given in the accepted answer and are far more compelling than compatibility with ancient, non-standard compilers. If there's an organization with coding standards that require casting `malloc`, those are some pretty poor standards. – Carey Gregory Aug 27 '16 at 21:49