0

Recently, I coded linked list with C++11. Using those concepts, I tried to code tree{a very basic tree} implementation in C++11. But it is giving me a Segmentation fault. I checked online and found out that it happens when the program tries to write a read access memory or tries to access free memory, but I can't figure out how it's happening here. Please help..

#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
    node *left;
    node *right;
    int key;
};
class tree{
    public:
    node *root;
    tree(){
        root->left=NULL;
        root->right=NULL;
    }
    node *createnode(int data){
        node *temp=new node;
        temp->key=data;
        temp->left=NULL;
        temp->right=NULL;
        return temp;
    }
};
int main(){
    tree t;
    node *root;
    root=t.createnode(1);
    //root->left=t.createnode(2);
    //root->right=t.createnode(3);
    //root->left->left=t.createnode(9);
    //root->left->right=t.createnode(7);
return 0;
}

check image

1
  • 4
    In tree() you access variable root, but you didn't initialize it. Commented Apr 25, 2018 at 17:30

2 Answers 2

5

t.root is uninitialized, so writing to root->left and root->right in tree's constructor invokes undefined behavior.

Right now, your tree class doesn't really make much sense. It contains a root node pointer, but you never use it. createnode could just be a free function or a static member of node. Better would be to encapsulate nodes entirely and just have tree have an insert method that takes an int, creates a new node, and inserts it at the appropriate place in the tree.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i initailised it and it worked. But I didn't get your last sentence. What do you mean by encapsulating node entirely? Can you give me an example..
@JimmyFails I mean make node private to tree so users of the tree class don't need to know about it or interact with it at all. Make it an implementation detail rather than part of tree's interface. For example, std::map has a node class that it uses internally, but to use std::map you don't have to know or think about nodes.
0

Your problem is in the constructor:

class tree{
    public:
    node *root;
    tree(){
        root->left=NULL;
        root->right=NULL;
    }

You declare root in the class, but you never initialize it. So when you dereference it in the body of the constructor, you are dereferencing a garbage pointer that could point to anything. Strictly speaking, what you are doing there is Undefined Behaviour and the compiler is well within its right to generate whatever it likes or even nothing at all for that bit of code.

Unrelated nitpick: don't use NULL in new code, use nullptr.

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.