Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7041035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:59:23+00:00 2026-05-28T01:59:23+00:00

This is my code to build a binary tree. From GDB debug message, recursive

  • 0

This is my code to build a binary tree. From GDB debug message, recursive call of insert_tree() works for 3 recursions and then *tree pointer points to an invalid address. Please help!

typedef struct _TNode{
  int data;
  struct _TNode *left;
  struct _TNode *right;
}TNode;

void insert_tree(TNode **tree,TNode *node)
{
  if(!(*tree)){
    *tree=node;
    return;
  }
  else if(node->data < (*tree)->data)
    insert_tree(&(*tree)->left,node);
  else if(node->data > (*tree)->data)
    insert_tree(&(*tree)->right,node);
}

TNode *build_btree(TNode **tree,int num)
{
  TNode *node;
  int i;
  for(i=0;i<num;i++){
    node=(TNode *)malloc(sizeof(TNode));

    node->left=NULL;
    node->right=NULL;
    node->data=rand();
    insert_tree(tree,node);
  }
  return *tree;
}

int main(int argc,char **argv)
{
  if(argc<2){
    printf("Usage: ./command <number of nodes to insert>\n");
    return -1;
  }
  TNode *root;
  int node_num=atoi(argv[1]);
  root=build_btree(&root,node_num);
  return 0;
}

This is GDB debug message. Don’t understand why *tree points to invalid address.

Program received signal SIGSEGV, Segmentation fault.
0x000000000040064a in insert_tree (tree=0x7fffffffe7d5, node=0x602010)
    at btree_height.c:9
9     else if(node->data < (*tree)->data)
(gdb) list
4   {
5     if(!(*tree)){
6       *tree=node;
7       return;
8     }
9     else if(node->data < (*tree)->data)
10      insert_tree(&(*tree)->left,node);
11    else if(node->data > (*tree)->data)
12      insert_tree(&(*tree)->right,node);
13  }
(gdb) print node->data
$1 = 1804289383
(gdb) print node
$2 = (TNode *) 0x602010
(gdb) print *tree
$3 = (TNode *) 0x6e69622f3d4c4c45
(gdb) backtrace
#0  0x000000000040064a in insert_tree (tree=0x7fffffffe7d5, node=0x602010)
    at btree_height.c:9
#1  0x0000000000400699 in insert_tree (tree=0x7fffffffe580, node=0x602010)
    at btree_height.c:12
#2  0x0000000000400699 in insert_tree (tree=0x7fffffffe480, node=0x602010)
    at btree_height.c:12
#3  0x00000000004006f7 in build_btree (tree=0x7fffffffe480, num=10)
    at btree_height.c:28
#4  0x000000000040082b in main (argc=2, argv=0x7fffffffe578) at btree_height.c:56
(gdb) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:59:24+00:00Added an answer on May 28, 2026 at 1:59 am

    Setting root to NULL removed the segfaults here.

    TNode *root = NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a binary tree, but from some reason my code doesn't
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>
I am trying to build a binary tree from a string input piped to
I've this code in my build.xml: <exec executable=cmd osfamily=winnt> <arg value=/c/> <arg value=xsltproc\bin\xsltproc.exe/> <arg
I got this code in order to build an url for the link using
I'm trying to build multilangual site. I use this piece of code to detect
I am getting this error when I try to build for the device: Code
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.