分段错误。为什么这个指针指向无效地址?

发布于 2024-12-25 11:27:53 字数 2163 浏览 0 评论 0原文

这是我构建二叉树的代码。从 GDB 调试消息来看,insert_tree() 的递归调用进行了 3 次递归,然后 *tree 指针指向无效地址。请帮忙!

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;
}

这是 GDB 调试消息。不明白为什么 *tree 指向无效地址。

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) 

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) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放肆 2025-01-01 11:27:53

root 设置为 NULL 删除了此处的段错误。

TNode *root = NULL;

Setting root to NULL removed the segfaults here.

TNode *root = NULL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文