解决的
问题最终源于数据结构的设计。要删除根元素,必须有一个堆分配(新)指针,这在数据由树直接保存的原始情况下是不可能的。现在有一个node
结构包含所有数据,tree
类包含所有方法和根指针。
我有一个初始化为target_address
的双指针NULL
,我必须稍后检查*target_address
是否为NULL。在这两者之间,函数保证*target_address
指向未初始化的值,但Valgrind一直抱怨我只是在删除根元素时才读取未初始化的值。
template <typename T>
int tree<T>::remove(T target) {
if (!this) // root is empty
return EMPTY;
tree<T>** target_address = NULL;
if (tree_find(target, &target_address) == SUCCESS) {
// expect there to be something in target_address
if (*target_address == NULL) return EMPTY; // <---- error happens
tree_delete(target_address);
return SUCCESS;
}
else return EMPTY;
}
对target_address
template <typename T>
int tree<T>::tree_find(T key, tree<T>*** target_address_handle) {
// find tree matched by key, or NULL pointer in correct location
// give tree pointer address back
tree<T>* root = this; // <---- ensures *target_address points to valid value, maybe this is problematic?
tree<T>** target_address = &root;
while(*target_address) {
tree<T>* current = *target_address;
if(typeid(key) == typeid(current->data)) {
// assume comparison operator exists
if(key == current->data)
break;
else if(key < current->data)
target_address = ¤t->left;
else
target_address = ¤t->right;
}
else return FAIL;
}
// if loop exited without breaking, will insert into an empty NULL position
// else loop exited by matching/breaking, will delete non-NULL tree
*target_address_handle = target_address;
return SUCCESS;
}
tree_delete也抱怨未初始化的价值;我很确定它也抱怨* target_address
void tree<T>::tree_delete(tree<T>** target_address) {
tree<T>* target = *target_address;
// first case: no left subtree, replace with right subtree (or NULL for a leaf)
if (!target->left)
*target_address = target->right;
// second case: no right subtree, replace with left subtree
else if (!target->right)
*target_address = target->left;
// third case: both subtrees, find second largest by taking rightmost left tree
else {
tree<T>** second_largest_address = &target->left; // start at target's left tree
while ((*second_largest_address)->right) // keep going right
second_largest_address = &((*second_largest_address)->right);
// reached the rightmost left
tree<T>* second_largest = *second_largest_address;
*target_address = second_largest; // delete target by replacing it with second largest
// second largest guranteed to not have a right subtree, so can treat as case 2 by shifting
*second_largest_address = second_largest->left;
second_largest->left = target->left;
second_largest->right = target->right;
}
delete target;
}
P.S。让Valgrind显示行号的提示将受到赞赏,汇编标志= -g -Wall -Werror -std=c++11
和Valgrind在-q --track-origins=yes
下运行
我按照其他人的问题建议静态链接-static
,但这引入了更多问题并且没有解决任何问题......
错误消息
==25887== Conditional jump or move depends on uninitialised value(s)
==25887== at 0x401764: tree<int>::remove(int) (in /home/johnson/Code/tree/treetest)
==25887== by 0x4013AA: main (in /home/johnson/Code/tree/treetest)
==25887== Uninitialised value was created by a stack allocation
==25887== at 0x40175A: tree<int>::remove(int) (in /home/johnson/Code/tree/treetest)
==25887==
After reading *target tree address
Before read
==25887== Use of uninitialised value of size 8
==25887== at 0x401A75: tree<int>::tree_delete(tree<int>**) (in /home/johnson/Code/tree/treetest)
==25887== by 0x40178E: tree<int>::remove(int) (in /home/johnson/Code/tree/treetest)
==25887== by 0x4013AA: main (in /home/johnson/Code/tree/treetest)
==25887== Uninitialised value was created by a stack allocation
==25887== at 0x401A46: tree<int>::tree_delete(tree<int>**) (in /home/johnson/Code/tree/treetest)
==25887==
==25887== Use of uninitialised value of size 8
==25887== at 0x401AA5: tree<int>::tree_delete(tree<int>**) (in /home/johnson/Code/tree/treetest)
==25887== by 0x40178E: tree<int>::remove(int) (in /home/johnson/Code/tree/treetest)
==25887== by 0x4013AA: main (in /home/johnson/Code/tree/treetest)
==25887== Uninitialised value was created by a stack allocation
==25887== at 0x401A46: tree<int>::tree_delete(tree<int>**) (in /home/johnson/Code/tree/treetest)
==25887==
==25887== Use of uninitialised value of size 8
==25887== at 0x401AF2: tree<int>::tree_delete(tree<int>**) (in /home/johnson/Code/tree/treetest)
==25887== by 0x40178E: tree<int>::remove(int) (in /home/johnson/Code/tree/treetest)
==25887== by 0x4013AA: main (in /home/johnson/Code/tree/treetest)
==25887== Uninitialised value was created by a stack allocation
==25887== at 0x401A46: tree<int>::tree_delete(tree<int>**) (in /home/johnson/Code/tree/treetest)
==25887==
==25887== Invalid read of size 8
==25887== at 0x401AF5: tree<int>::tree_delete(tree<int>**) (in /home/johnson/Code/tree/treetest)
==25887== by 0x40178E: tree<int>::remove(int) (in /home/johnson/Code/tree/treetest)
==25887== by 0x4013AA: main (in /home/johnson/Code/tree/treetest)
==25887== Address 0x801f0fc36d is not stack'd, malloc'd or (recently) free'd
答案 0 :(得分:2)
更糟糕的是,请查看if (!this) return EMPTY;
内的代码:
tree_find
显然tree<T>* root = this; // <-- ensures *target_address points to stack variable !
tree<T>** target_address = &root; // Set out pointer to stack variable
不存在,然后root
返回。毫无疑问,这是一个错误。