不知道为什么 valgrind 给我 Conditional jump or move depends on uninitialised value(s) 错误?
Don't know why valgrind gives me Conditional jump or move depends on uninitialised value(s) error?
我下面的代码成功地将二叉搜索树转换为链表,但是 valgrind 一直给我 "Conditional jump or move depends on uninitialised value(s)" 错误。然而在查看我的代码后,我没有看到我在哪里?
ListNodePtr convertBSTtoLinkedList(TreeNodePtr root)
{
ListNodePtr list,head;
list = malloc(sizeof(struct ListNode));
list->key = root->key;
if (root->right != NULL)
{
list->next = convertBSTtoLinkedList(root->right); //line 80
}
if (root->left != NULL)
{
ListNodePtr tail;
head = convertBSTtoLinkedList(root->left); //line 85
tail = head;
while (tail->next != NULL) { //Line 87
tail = tail->next;
}
tail->next = list;
return head;
}
return list;
}
这是我的 valgrind 错误,它会重复几次。
==3076== Conditional jump or move depends on uninitialised value(s)
==3076== at 0x108AF2: convertBSTtoLinkedList (bst.c:87)
==3076== by 0x108AC8: convertBSTtoLinkedList (bst.c:80)
==3076== by 0x108ADD: convertBSTtoLinkedList (bst.c:85)
==3076== by 0x108ADD: convertBSTtoLinkedList (bst.c:85)
==3076== by 0x108ADD: convertBSTtoLinkedList (bst.c:85)
==3076== by 0x108AC8: convertBSTtoLinkedList (bst.c:80)
==3076== by 0x108AC8: convertBSTtoLinkedList (bst.c:80)
==3076== by 0x108754: main (main_bst.c:28)
malloc
为您提供指向未初始化内存的指针。因此,除非您设置 ListNode
结构的每个成员,否则稍后尝试访问该成员是一个坏主意。
当您的 convertBSTtoLinkedList
函数处理 BST 的任何没有权限 child 的节点时,它无法在创建的列表节点上设置 next
。所以下一次递归中的更高级别试图找到返回的子列表的末尾,它走到子列表的末尾,检查未初始化的 next
指针,并且 valgrind 抱怨。
我下面的代码成功地将二叉搜索树转换为链表,但是 valgrind 一直给我 "Conditional jump or move depends on uninitialised value(s)" 错误。然而在查看我的代码后,我没有看到我在哪里?
ListNodePtr convertBSTtoLinkedList(TreeNodePtr root)
{
ListNodePtr list,head;
list = malloc(sizeof(struct ListNode));
list->key = root->key;
if (root->right != NULL)
{
list->next = convertBSTtoLinkedList(root->right); //line 80
}
if (root->left != NULL)
{
ListNodePtr tail;
head = convertBSTtoLinkedList(root->left); //line 85
tail = head;
while (tail->next != NULL) { //Line 87
tail = tail->next;
}
tail->next = list;
return head;
}
return list;
}
这是我的 valgrind 错误,它会重复几次。
==3076== Conditional jump or move depends on uninitialised value(s)
==3076== at 0x108AF2: convertBSTtoLinkedList (bst.c:87)
==3076== by 0x108AC8: convertBSTtoLinkedList (bst.c:80)
==3076== by 0x108ADD: convertBSTtoLinkedList (bst.c:85)
==3076== by 0x108ADD: convertBSTtoLinkedList (bst.c:85)
==3076== by 0x108ADD: convertBSTtoLinkedList (bst.c:85)
==3076== by 0x108AC8: convertBSTtoLinkedList (bst.c:80)
==3076== by 0x108AC8: convertBSTtoLinkedList (bst.c:80)
==3076== by 0x108754: main (main_bst.c:28)
malloc
为您提供指向未初始化内存的指针。因此,除非您设置 ListNode
结构的每个成员,否则稍后尝试访问该成员是一个坏主意。
当您的 convertBSTtoLinkedList
函数处理 BST 的任何没有权限 child 的节点时,它无法在创建的列表节点上设置 next
。所以下一次递归中的更高级别试图找到返回的子列表的末尾,它走到子列表的末尾,检查未初始化的 next
指针,并且 valgrind 抱怨。