1

I'm trying to learn some C and I started by implementing a binary tree.

The first thing I'm trying is to insert into the tree. However, I'm getting a segmentation fault and I can't figure out why.

The segmentation fault happens when I try to set the *tree parameter to a new node with the passed in value. I tried using free(*tree) first but that doesn't really help.

From what I understand I have a pointer to the pointer of my tree. So I should update the pointer that is being pointed to (if this makes sense?). createNode(val) returns a pointer to a node. So I figure I can simply put that pointer in place of the 'pointee'. So I have a pointer to a new pointer, which was intially NULL.

I'm aware that I don't really get how pointers really work at this point, but that's why I'm trying.. #include #include #include

typedef struct node {
    int val;
    struct node * left;
    struct node * right;
}node;

node * createNode(int val)
{
    node * n = malloc(sizeof(node));
    n->val = val;
    n->left = NULL;
    n->right = NULL;

    return n;
}
node * createTree()
{
    node * root = malloc(sizeof(node));
    root->val = 12;
    root->left = createNode(10);
    root->right = createNode(14);

    root->left->left = createNode(8);
    root->left->right = createNode(9);

    root->right->left = createNode(13);
    root->right->right = createNode(16);

    return root;
}
void insertInTree(node ** tree, int val)
{
    printf("%i\n", (*tree)->val);

    if((*tree) == NULL)
    {
        free(*tree);
        *tree = createNode(val);
    }
    else
    {
        if((*tree)->val > val)
        {
            insertInTree(&(*tree)->left, val);
        }
        else
        {
            if((*tree)->val < val)
            {
                insertInTree(&(*tree)->right, val);
            }
        }
    }   
}

main()
{
   node * tree = createTree();
   insertInTree(&tree, 5);
   getchar();

}
5
  • root->left->left = createNode(8); root->left->right = createNode(9); Structure of the tree is not correct. Commented Feb 4, 2014 at 10:15
  • why are you explicitly calling free if a node is NULL (if ((*tree) == NULL) { free(*tree);? There's no point Commented Feb 4, 2014 at 10:16
  • 1
    freeing NULLs is legal, but it has no effect. Commented Feb 4, 2014 at 10:16
  • @dasblinkenlight: I know, but if it's a NULL pointer, the free call has no point. If it's not optimized away, all it does is add pointless function calls Commented Feb 4, 2014 at 10:17
  • Yes, that was something I tried afterwards. I figured it to be pointless :) Commented Feb 4, 2014 at 10:18

1 Answer 1

4

In insertInTree()

printf("%i\n", (*tree)->val);

you get a SEGFAULT after visit the last node (NULL)

A debugger will tell you that:

david@debian:~$ gdb demo
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/david/demo...done.
(gdb) run
Starting program: /home/david/demo 
12
10
8

Program received signal SIGSEGV, Segmentation fault.
0x00000000004006ea in insertInTree (tree=0x601078, val=5) at demo.c:36
36      printf("%i\n", (*tree)->val);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.