分段错误。为什么这个指针指向无效地址?
这是我构建二叉树的代码。从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
root
设置为NULL
删除了此处的段错误。Setting
root
toNULL
removed the segfaults here.