0

I am trying to create a function that will malloc my node pointers but for whatever reason the passed node when accessed after my function throws an error as if the node was not given memory at all. Here is my code:

typedef struct node
{
    bool is_word;
    char chars[N];
    struct node *children[N];
}
node;

node *root;

bool alloc_memory(node *n)
{
    n = malloc(sizeof(node));
    if (n == NULL)
    {
        return false;
    }
    for (int i = 0; i < N; i++)
    {
        n->children[i] = NULL;
    }

    return true;
}

bool load(const char *dictionary)
{
    // Initialize trie
    bool t = alloc_memory(root);
    if (!t)
    {
        return false;
    }

    root->is_word = false;

...
}

At root->is_word = false; I get the following two errors:

runtime error: member access within null pointer of type 'node' (aka 'struct node')

runtime error: store to null pointer of type 'bool'

What am I missing to correctly initialize my node with malloc?

1 Answer 1

0
bool alloc_memory(node *n)

Your parameter n is an independent variable, initialised to whatever you pass to alloc_memory. So any changes to n won't be reflected in the calling code.

There are at least two common ways around:

  1. Return the pointer, not a boolean. If the pointer returned is NULL, you know something went wrong.
  2. Make the parameter a pointer to pointer to node. That way, you pass the memory address of your pointer you want to have pointing to the new node. The pointer to pointer is still a copy, but the pointee is not.

And I guess you don't need char chars[N];.

Edit: Some examples what this could look like

  1. Function:

    node *make_node() {
        node *new_node = malloc(sizeof(node));
        if (new_node) {
            // initialise the node
        }
        return new_node;
    }
    

    Calling code:

    node *my_new_node = make_node();
    if (!my_new_node) {
        // something's going terribly wrong
    }
    
  2. Function:

    bool make_node(node **node_ptr) {
        node *new_node = malloc(sizeof(node));
        if (new_node) {
            // initialise the node
    
            *node_ptr = new_node;
            return true;
        } else {
            return false;
        }
    }
    

    Calling code:

    node *my_new_node;
    if (!make_node(&my_new_node)) {
        // something's going terribly wrong
    }
    
3
  • I didn't know that a double pointer or a pointer to a pointer was a thing. So you are saying when I pass in the memory address that is root it takes this address and copies it to a different address inside of alloc_memory? Why then can I take the function using the signature bool alloc_memory(int newarray[]) - pass in an array and the array modifications will be saved after the function is finished? An array is a pointer - why do I have to use a pointer to a pointer in this situation and not in another?
    – theurere
    Feb 18, 2019 at 18:58
  • In the array case, you are modifying the pointee, not the pointer passed.
    – Blauelf
    Feb 18, 2019 at 19:00
  • @theurere I added some example code how this would work. You passing an array is equivalent to the second (the array is a pointer, accessing its element is dereferencing)
    – Blauelf
    Feb 19, 2019 at 20:24

You must log in to answer this question.

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