问题描述
我在二进制搜索树中删除一些数据时可以免费获得段错误。
来自gdb的后退跟踪如下。
#1 0x0036fe30 in raise()from /lib/libc.so.6
(gdb)up
#2 0x00371741 in / from /lib/libc.so.6
(gdb)up / /
#3 0x003a88cb in __libc_message()from /lib/libc.so.6 br />
(gdb)up
#4 0x003b0c65 in _int_free()from /lib/libc.so.6
(gdb)up
#5 0x003b4c59 in free()from /lib/libc.so.6
(gdb)up
#6 0x0804a014 in Delete_Node(dest = 4854128) ,del_trav_node = 0x1804e158,dest_std = 4854128)at src / smsc_bintree_operation.c:297
297 src / smsc_bintree_operation.c:没有这样的文件或目录。
src / smsc_bintree_operation中的
.c
(gdb)p del_trav_node-> left
$ 1 =(struct bst *)0x0
(gdb)p del_trav_node-> ;对吧
$ 2 =(struct bst *)0xebb000
如需参考,删除代码如下
NODE Delete_Node(U32bit dest,NODE del_trav_node,U32bit dest_std)
{
NODE del_trav_temp,del_trav_prev;
if (del_trav_node == NULL)
{
LOG( \ notlement not found \\\
);
}
else
{
if (dest < del_trav_node-> key_val)
{
del_trav_prev = del_trav_node;
del_trav_node-> left = Delete_Node(dest,del_trav_node-> left,dest_std);
}
其他 if (dest> del_trav_node-> ; key_val)
{
del_trav_prev = del_trav_node;
del_trav_node-> right = Delete_Node(dest,del_trav_node-> right,dest_std);
}
其他 // 如果要删除的节点是根节点
{
if ((del_trav_node-> left = = NULL)&&(del_trav_node-> right == NULL))
{
free(del_trav_node);
return (NULL);
}
else if ((del_trav_node-> left!= NULL) &&(del_trav_node-> right!= NULL))
{
del_trav_temp = Find_Min(del_trav_node-> right);
del_trav_node-> key_val = del_trav_temp-> key_val;
memcpy(& del_trav_node-> value,& del_trav_temp-> value, sizeof (SMSC_DATA_ARRAY));
del_trav_node-> right = Delete_Node(del_trav_node-> key_val,del_trav_node-> right,dest_std);
}
else if (del_trav_node-> left == NULL)
{
del_trav_temp = del_trav_node;
del_trav_node = del_trav_node-> right;
if (del_trav_temp!= NULL)
free(del_trav_temp);行号--- 297
}
else if (del_trav_node-> right == NULL)
{
del_trav_temp = del_trav_node;
del_trav_node = del_trav_node-> left;
if (del_trav_temp!= NULL)
free(del_trav_temp);
}
else ;
}
}
return del_trav_node;
};
任何帮助将不胜感激。
谢谢
SP
代码块添加 - OriginalGriff [/ edit]
Hi ,
I am getting segfault on free while deleting some data in binary search tree.
The back trace from gdb is as follows.
#1 0x0036fe30 in raise () from /lib/libc.so.6
(gdb) up
#2 0x00371741 in abort () from /lib/libc.so.6
(gdb) up
#3 0x003a88cb in __libc_message () from /lib/libc.so.6
(gdb) up
#4 0x003b0c65 in _int_free () from /lib/libc.so.6
(gdb) up
#5 0x003b4c59 in free () from /lib/libc.so.6
(gdb) up
#6 0x0804a014 in Delete_Node (dest=4854128, del_trav_node=0x1804e158, dest_std=4854128) at src/smsc_bintree_operation.c:297
297src/smsc_bintree_operation.c: No such file or directory.
in src/smsc_bintree_operation.c
(gdb) p del_trav_node->left
$1 = (struct bst *) 0x0
(gdb) p del_trav_node->right
$2 = (struct bst *) 0xebb000
For a reference the code for deletion is as follows
NODE Delete_Node(U32bit dest,NODE del_trav_node, U32bit dest_std) { NODE del_trav_temp,del_trav_prev; if(del_trav_node == NULL) { LOG("\nelement not found\n"); } else { if(dest < del_trav_node->key_val) { del_trav_prev = del_trav_node; del_trav_node->left = Delete_Node(dest,del_trav_node->left,dest_std); } else if(dest > del_trav_node->key_val) { del_trav_prev = del_trav_node; del_trav_node->right = Delete_Node(dest,del_trav_node->right,dest_std); } else //if the node to be deleted is the root node { if((del_trav_node->left == NULL) && (del_trav_node->right == NULL)) { free(del_trav_node); return(NULL); } else if((del_trav_node->left != NULL) &&(del_trav_node->right != NULL)) { del_trav_temp = Find_Min(del_trav_node->right); del_trav_node->key_val = del_trav_temp->key_val; memcpy(&del_trav_node->value,&del_trav_temp->value,sizeof(SMSC_DATA_ARRAY)); del_trav_node->right = Delete_Node(del_trav_node->key_val,del_trav_node->right,dest_std); } else if(del_trav_node->left == NULL) { del_trav_temp = del_trav_node; del_trav_node = del_trav_node->right; if(del_trav_temp != NULL) free(del_trav_temp); line no ---297 } else if(del_trav_node->right == NULL) { del_trav_temp = del_trav_node; del_trav_node = del_trav_node->left; if(del_trav_temp != NULL) free(del_trav_temp); } else; } } return del_trav_node; };
Any help will be appreciated .
Thanks
SP
[edit]Code block added - OriginalGriff[/edit]
这篇关于在二叉树中删除数据时自由段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!