Segmentation fault (core dumped) - Threaded Binary Search Tree

I keep getting the following error : Segmentation fault (core dumped) . I found out the line of code that is causing the problem ( marked with a comment inside of the program) . Please tell me why this error is happening and how to fix it.

I've tried to dry run my code (on paper ) and see no logical errors (from my understanding). I have only recently got into coding and stackoverflow please guide me through how I can further improve my question , as well as my code . Thanks !

class tree
{
struct node    // Creates a node for a tree
{
    int data;
    bool rbit,lbit;  // rbit/lbit= defines if right/left child of root is present or not
    node *left,*right;
};
public:
    node *head,*root;
    tree() // constructor initializes root and head  
    {
        root=NULL;
        head=createnode(10000);
    }
    node *createnode(int value)  
    {// Allocates memory for a node then initializes node with given value and returns that node
        node *temp=new node ;
        temp->data=value;
        temp->lbit=0;
        temp->rbit=0;
        temp->left=NULL;
        temp->right=NULL;
        return temp;
    }
    void insert(node *temp,int value) // Creates binary search tree node by node
    {
        if(root==NULL)  // Checking if tree is empty
        {
            root=createnode(value);  //Root now points to new memory location 
            head->left=root;
            head->lbit=1;
            root->left=head;//this line gives the segmentation fault (what i thought before correction) 
        }   

    }
    void inorder(node *root) // Inorder traversal of tree (this function is logically incorrect) 
    {
        if(root==NULL)
            return;
        inorder(root->left);
        cout<<root->data<<"\t";
        inorder(root->right);
    }
    void getdata()//Accepts data , creates a node through insert() , displays result through inorder()
    {
        int data;
        cout<<"Enter data"<<endl;
        cin>>data;  
        insert(root,data);
        inorder(root);
    }
 /*void inorder(node *root) // Working inorder code
{
    if(root->lbit==1)
    inorder(root->left);
    cout<<root->data<<"\t";
    if(root->rbit==1)
    inorder(root->right);
}*/
};
int main()
{

    tree t; // Tree Object 
    t.getdata();  // Calling getdata
    return 0;
}

I think the comments section largely reflects a miscommunication. It's easy to believe that you are experiencing a crash ON that particular line.

This is not actually the case. Instead what you have done is created a loop in your tree which leads to infinite recursion by the inorder function. That causes a stack overflow which segfaults -- this would have been extremely easy to spot if you had just run your program with a debugger (such as gdb) attached.

temp = createnode(value);
if(root == NULL)
{
    root = temp;
    head->left = root;
    head->lbit = 1;
    temp->left = head;
}   

Look at the loop you have just created:

head->left points to root
root->left == temp->left, which points to head

An inorder traversal will now visit:

root
  head
    root
      head
        root
          head
            ...

Since it never gets to the end of the left-branch, the function never outputs anything before overflowing the stack and crashing.

So no, your code is not logically correct. There's a fundamental design flaw in it. You need to rethink what you are storing in your tree and why.

c++, Segmentation fault (core dumped) - Threaded Binary Search Tree - c++. temp; } void insert(node *temp,int value) // Creates binary search tree node by node  Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump. It is an error indicating memory corruption. Common segmentation fault scenarios:

From the code,

    root=temp;  //Root now points to temp
    head->left=root;
    head->lbit=1;
    temp->left=head;// this line gives the segmentation fault 

root is not pointing to temp. temp(pointer) is assigned to root(pointer). head's left pointer is root, and temp's left is head (which means root's left is head). so in the function "inorder",

    void inorder(node *root) // Inorder traversal of tree
    {
        if(root==NULL)             <<<<<<
            return;
        inorder(root->left);
        cout<<root->data<<"\t";
        inorder(root->right);
    }

the argument node *root (left) is never NULL and the function never return.

C++ BST segmentation fault, Segfaults are memory access violations. Try to find on exactly which row the segfault occurs by printing stuff before and after a row that you suspect may be  Segmentation fault (core dumped) My program makes use of templated classes but I didn't include any of the .h code in order to keep this post as small as possible. Any help would be great.

There's not enough information on exactly how this should work (what is node.lbit for example).

The question's insert() function will not work. It's passing in a value which is immediately overwritten (among other issues). There's no explanation of what tree.head is for, so it's ignored. The fields node.lbit and node.rbit look to be superfluous flags of node.left != NULL (similarly for right). These are omitted too. The insert() is also not creating the tree properly.

void insert(int value) // Insert a value into the tree (at branch)
{
    // Create a new node to insert
    struct node *temp = createnode(value);

    if (root == NULL)  // Checking if tree is empty
    {
        root = temp;  //Root now points to temp
    }  
    else 
    {
        insertAtBranch(root, temp);
    }
}

// recursively find the leaf-node at which to insert the new node
void insertAtBranch(node *branch, node *new_node)
{
    // to create a BST, less-than go left
    if (new_node->value <= branch->value)
    {
        if (branch->left == NULL)
            branch->left = new_node;  // There's no left-branch, so it's the node
        else
            insertAtBranch(branch->left, new_node);  // go deeper to find insertion point
    }
    else // greater-than go right
    {
        if (branch->right == NULL)
            branch->right = new_node;
        else
            insertAtBranch(branch->right, new_node);
    }
}         

Imagine how a binary tree works. New nodes are only ever inserted at the edges. So you look at a given node, and decide if this new-node is less or grater than the one you're looking at (unless the tree is empty, of course).

Say the new-node.value is less than the branch-node.value, you want to branch left. Still with the same node, if it doesn't have a left-branch (node.left == NULL), the new node is the left branch. Otherwise you need to travel down the left-branch and check again.

I would have made node a class, and used a constructor to at least set the default properties and value. But that's not a big deal.

C++ Tutorial: Debugging Crash & Memory Leak- 2020, Segmentation fault (core dumped) - Threaded Binary Search Tree class tree { struct node // Creates a node for a tree { int data; bool rbit,lbit;  System information Have I written custom code (as opposed to using a stock example script provided in TensorFlow): OS Platform and Distribution: Linux Ubuntu 18.04 TensorFlow installed from (source or binary): bynary (tensorflow-gpu) Ten

CS50 Quiz 0 n Flashcards, So when I run it it says: "Segmentation fault (core dumped). Here is the code: Code: [View]. //BTree#include <iostream> using namespace std; //  Sadly during 'printing' this tree I came across a problem which is core dumped. I don't know why but one of the structurs "has" (*korzen)->left that isn't NULL, my funcion wants to acess it,and the core dumped appears.

Server crashes with Segmentation fault when running SPARQL , C++ Tutorial: Debugging Crash & Memory Leak, core dump, segmentation fault, gdb, dangling pointer, uninitialized memory, and buffer overflow. Find answers to Segmentation fault (core dumped) error - C program for Binary Search Tree : Level Order Traversal from the expert community at Experts Exchange

Search, A multi-threaded program can perform multiple tasks at once. It splits its functionality Name two ways in which you can induce a segmentation fault. By indexing into array Binary Search, Linear Search. How does What does " Segmentation fault (core dumped)" mean? What is the code for a binary tree? struct node To my knowledge, this should just generate random numbers, insert them in a binary tree, and then the traverse function should print them out. When I compile everything goes fine but when I run the program it just says "Segmentation fault (core dumped)."

Comments
  • You're asking if a given pointer is NULL, and then trying to access it? It's kinda like playing Russian Roulette, except rather than taking chances, you fill the gun with bullets to make sure you'll get shot.
  • That's why i created the variable temp( temp assigns a block of node memory via the function createnode(value) ) . I check if my root node is NULL and if it is I immediately assigned it to temp. That means the root pointer is now pointing to a valid memory block . I am trying to access that memory block 's "left "pointer and assign it to a new address ( "head" node's address )
  • Sorry, missed the root = temp part. Anyway: you're calling insert(root,data), yet the first argument of insert is temp itself which is immediately overwritten. You should think this through as it makes no sense.
  • Another thing that makes no sense: If root is not NULL, insertnode() does absolutely nothing, whatsoever, except leak memory.
  • Actually i've tried this code 3 times( 2 times in college and one time at home) , what I initially did was root=createnode(value) and had the same error both places . So I decided to do the code a bit differently this time around . Although this makes no sense , the code should at least work logically right? Once it at least shows me an output I'll brush up the code .
  • Thank you !!! I will learn how to use a debugger . I've fixed the error in inorder , this time i checked if they have lbit/rbit=1 before calling the function instead of checking if root was NULL