1

I'm new to C and work myself throug the MIT Open Courseware for practical C programming (MIT OCW Homepage) in order to learn some C basics.

In assignment 5 we are supposed to implement a small binary tree library for allocation, deallocation, display and traversal.

While testing the following implementation

#include <stdlib.h>

struct TreeNodeStruct {
        int data ;
        struct TreeNodeStruct *left, *right ;
} ;

typedef struct TreeNodeStruct TreeNode ;

TreeNode* talloc(int data)
{

    /* variables */

    TreeNode *p ;

    /* logic */

        p = malloc(sizeof(struct TreeNodeStruct)) ;
        p->data = data ;

    /* return */

    return p ;

}

TreeNode* addnode(TreeNode* root ,int data)
{

    /* logic */

        if(root == NULL) return talloc(data) ; // allocate node and return as new root
        else if(data < root->data) root->left = addnode(root->left, data) ;
        else root->right = addnode(root->right, data) ;

    /* return */

    return root ;

}

in a very simple test case

printf("\nProblem 5.2\n") ;

tree = talloc(0) ;

printf("talloc(0): %s\n", tree == NULL ? "failure" : "success") ;

for(int i1 = 0, i2 = 10; ++i1 < i2 ; ) addnode(tree, i1) ;

I run into a segmentation fault 11 error. Is this an artefact of my test case or an error in my implementation? If it is, what am I doing wrong?

2
  • 1
    Use calloc instead of malloc. It will initialize the members of the node with zeros. And use sizeof *p instead of sizeof(struct TreeNodeStruct). sizeof is compile-time. Commented Mar 19, 2015 at 15:40
  • Alright, simple problem, simple solution. Thanks for the quick answers! Commented Mar 19, 2015 at 15:42

3 Answers 3

2

malloc doesn't initialize the members of the struct. Add code to do that.

p = malloc(sizeof(struct TreeNodeStruct)) ;
p->data = data ;
p->left = NULL;
p->right = NULL;

You can also use calloc if you don't want to have those lines.

p = calloc(1, sizeof(struct TreeNodeStruct)) ;
p->data = data ;
Sign up to request clarification or add additional context in comments.

Comments

2

In talloc you need to set left and right to NULL.

When your code iterates through the binary tree, it will continue to call itself recursively until it finds NULL, which indicates you are on a "leaf". But since left and right contain garbage, it won't find NULL.

2 Comments

Ok, yeah i get it now. left and right weren't initialized. Why aren't they initialized to 0 though? Or, more to the point, why do they point to "garbage"?
@FK82 Because malloc does nothing but allocating a chunk of memory as fast as possible. It does not initialize the allocated memory to any known values. Unlike calloc, which is guaranteed to initialize the allocated memory to zero.
2

You didn't initialize left and right to NULL in talloc(). addnode() cannot identify non-existing subtree correctly.

You should note that malloc() does not initialize anything in the allocated memory space. To initialize it into 0 automatically, you should use calloc().

p = calloc(1, sizeof(TreeNode));

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.