二进制搜索树,Valgrind条件跳转或移动取决于未初始化的值

时间:2018-10-10 07:51:01

标签: c pointers initialization binary-search-tree valgrind

我正在尝试实现bst及其运行所需的所有功能(插入,删除,遍历,delete_tree)。

一切似乎都正常。但是Valgrind抱怨未初始化的值,但是我在示例中将如何以及如何初始化。我读过其他一些讨论此问题的主题,但在将其应用于示例时遇到了麻烦。

我们非常感谢您的帮助。谢谢

这是我的代码:

node* insert(node* root, int key)
{
  if(root == NULL){
      root = malloc(sizeof(node));
      if(root != NULL){
          root->key_ = key;
      }

  }
  else{

      if(key == root->key){
        return NULL;
      }
      else if(key < root->key){
        if(root->left_ != NULL ){
          insert(root->left_, key);
        }
        else{
            root->left_ = NULL;
            node* newNode = malloc(sizeof(node));
            if(newNode != NULL){
                newNode ->key_ = key;
                root->left_ = newNode;
            }

        }
      }
      else{
        if(root->right_ != NULL){
            insert(root->right_, key);
        }
        else{
            root->right_ = NULL;
            node* newNode = malloc(sizeof(node));
            if(newNode != NULL){
                newNode ->key_ = key;
                root->right_ = newNode;
            }

        }
      }
  }


  return root;
}


node* search(node* root, int key)
{ 
  if(key == root->key){
      return root;
  }
  else{
      if(key < root->key{
          if(root->left_ != NULL){
              search(root->left_,key);
          }
          else{
              return NODE_NOT_FOUND;
          }
      }
      else{
          if(root->right_ != NULL){
              search(root->right_,key);
          }
          else{
              root->right_ = NULL;
              return NODE_NOT_FOUND;
          }
      }
  }


  return root;

}

node* findMin(node* root){
    while(root->left_ != NULL){
        root = root->left_;
    }
    return root;
}

node* delete(node* root, int key)
{
 if(key < root->key){
     root->left_ = delete(root->left_,key);
 }
 else if (key > root->key){
     root->right_ = delete(root->right_,key);
 }
 else{
     if(root->left_ == NULL && root->right_ == NULL){
         free(root);
         root = NULL;
     }
     else if(root->left_ == NULL){
         node* temp = root;
         root = root->right_;
         free(temp);
     }
     else if(root->right_ == NULL){
         node* temp = root;
         root = root->left_;
         free(temp);
     }
     else{
         node* temp = findMin(root->right_);
         root->key_= temp->key_;
         root->right_= delete(root->right_,temp->key_);
     }
 }
return root;
}
void delete_tree(node* root)
{
  if(root != NULL){
      delete_tree(root->left_);
      delete_tree(root->right_);
      root->left_ = NULL;
      root->right_ = NULL;
      free(root);
  }
}

Valgrind输出:

==6289== Memcheck, a memory error detector
==6289== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6289== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6289== Command: ./bst.out
==6289== 
==6289== Conditional jump or move depends on uninitialised value(s) [gone]
==6289==    at 0x108816: insert (bst.c:64)
==6289==    by 0x108B42: main (main.c:12)
==6289== 
==6289== Conditional jump or move depends on uninitialised value(s) [gone]
==6289==    at 0x1087B7: insert (bst.c:50)
==6289==    by 0x108B57: main (main.c:13)
==6289== 
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289==    at 0x1089DA: delete (bst.c:139)
==6289==    by 0x1089BE: delete (bst.c:136)
==6289==    by 0x108C0B: main (main.c:17)
==6289== 
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289==    at 0x1089E7: delete (bst.c:139)
==6289==    by 0x1089BE: delete (bst.c:136)
==6289==    by 0x108C0B: main (main.c:17)
==6289== 
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289==    at 0x1089DA: delete (bst.c:139)
==6289==    by 0x10897E: delete (bst.c:133)
==6289==    by 0x108C20: main (main.c:18)
==6289== 
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289==    at 0x1089E7: delete (bst.c:139)
==6289==    by 0x10897E: delete (bst.c:133)
==6289==    by 0x108C20: main (main.c:18)**
==6289== 
==6289== 
==6289== HEAP SUMMARY:
==6289==     in use at exit: 0 bytes in 0 blocks
==6289==   total heap usage: 3 allocs, 3 frees, 72 bytes allocated
==6289== 
==6289== All heap blocks were freed -- no leaks are possible
==6289== 
==6289== For counts of detected and suppressed errors, rerun with: -v
==6289== Use --track-origins=yes to see where uninitialised values come from
==6289== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:1)

不确定是否可以涵盖您的所有问题,但这里

if(root == NULL){
  root = malloc(sizeof(node));
  if(root != NULL){
      root->key_ = key;
  }

我希望这样:

if(root == NULL){
   root = malloc(sizeof(node));
   if(root != NULL){
      root->key_ = key;
      root->left_ = NULL;    // new initialization
      root->right_ = NULL;   // new initialization
  }