Visual Studio 2010调试"if(var == NULL)"未触发

Wal*_*ter 6 c++ null if-statement visual-studio-2010

解决了 - 构造函数的问题

Matthew FlaschenMichael Burr指出了重载的Node(int)调用构造函数的问题,Node()这是不行的,因为... 谢谢你们!


我已经构建了一个程序(我正在调试它)并且遇到了一个奇怪的问题......一个`if`语句没有被触发它应该是......这是一个学校项目,我们必须用它构建一个AVL树至少有一个'优化'功能.

我确信并且已经测试过`rdown`和`ldown`工作(作为平衡因素) - 树不是完全平衡的.相反,它是基于分支的高度(即 - `balance()`应该只返回(1,0,-1)否则它是不平衡的.

我希望这是解决这个奇怪问题的足够信息......在使用Microsoft Visual Studio 2010之前,我从未遇到类似这样的事情.

节点结构:

struct Node {
    int data;           // the data in the Node
    int rdown;          // the number of ellements below the node on the right side
    int ldown;          // the number of ellements below the node on the left side
    Node * parrent;     // the node's parrent
    Node * lchild;      // the nodes left child
    Node * rchild;      // the nodes right child

    Node () { rdown = 0, ldown = 0; data = 0; parrent = NULL; lchild = NULL; rchild = NULL; }
    Node (int dat) {rdown = 0, ldown = 0; parrent = NULL; lchild = NULL; rchild = NULL; data = dat; } 
    bool end() { if (lchild == NULL && rchild == NULL) return true;     // check if this node is the 'end of the line' - where it doesn't
                 return false; }                                        // have any children
    bool goodToAdd() { if (lchild == NULL || rchild == NULL) return true;   // make sture the current node has at least one spot to add
                       return false; }                                      // a new node to - either lchild or rchild must be NULL

    int balance() { return (ldown - rdown); }                           // get a balance number for the node
};
Run Code Online (Sandbox Code Playgroud)

搜索导致问题的功能

Node * AVL_Tree::search(const Node * num) {
 Node * tmpNode = AVL_Tree::root;      // tmpNode is a place holder for the search
 for (int i = 1; true; i++) {     // increment int i to check for excess searching -> pervents endless loop
  if (tmpNode == NULL) //****** causing problems********  // the search has reached a dead end (the data is not contained) ==>  NULL
   return NULL;
  if (tmpNode->data == num->data)   // if the data of num is the same as tmpNode the data is contained ==>  Node *
   return tmpNode;
            // since the node has not been found yet move down the tree...
  if (tmpNode->data > num->data && tmpNode->lchild != NULL) // if the data is smaller than the tmpNode move to the lchild
   tmpNode = tmpNode->lchild;
  else if (tmpNode->rchild != NULL)    // since the node has been proven to not be = to the data to be searched for
   tmpNode = tmpNode->rchild;    // and it is not smaller... move to the right

  if (i > (root->ldown + 1) && i > (root->rdown + 1) ) {  // the while loop has searched suffecent time and has not ended
   string tmp = "the search incountered a critical error... aborting..."; // to prevent an endless loop the string error
   throw tmp;            // is thrown (should not happen) - indicates a broken tree
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

第一次遭遇的屏幕截图 for loop

替代文字

第二次遭遇的屏幕截图 for loop

如果您在底部的"Autos"选项卡中注意到所有数据和节点本身的地址是NULL- 但在下一个屏幕截图中它继续 替代文字

该计划继续!!! 什么?>!

我按下了F-10('转到下一个命令'按钮)......然后它跳过了声明?为什么? 替代文字

Mic*_*urr 4

0xcdcdcdcd不是 NULL 指针 - 该值在 MSVC 的调试版本中用于已分配但未初始化的内存。

请参阅操作系统何时以及为什么会在 malloc/free/new/delete 上将内存初始化为 0xCD、0xDD 等?更多细节。

问题的根源可能在于带有int参数的构造函数:

Node (int dat) { Node(); data = dat; } 
Run Code Online (Sandbox Code Playgroud)

Node();声明最终什么也没做。此构造函数使结构的大多数成员未初始化。