Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 Debugging "if (var == NULL)" not triggering

Solved - Problem with constructor

Matthew Flaschen and Michael Burr pointed out the problem of the overloaded constructor of Node(int) calling Node() which doesn't work because... Thanks guys!


I have built a program (I am debugging it) and have run into a weird problem... A `if` statement is not getting triggered when it should be... This is a school project where we must build an AVL Tree with at least one 'optimizing' feature.

I am sure and have tested that the `rdown` and `ldown` work (as the balancing factors) - the tree is not perfectly balanced. Rather it is based on the hight of the branches (i.e. - `balance()` should only return (1,0,-1) otherwise it is unbalanced.

I hope this is enough information to solve this weird problem... I have never ran into anything like this before with Microsoft Visual Studio 2010.

Node struct:

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
};

Search function that is causing the problems

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
  }
 }
}

A screen shot of the first encounter with the for loop

alt text

A screen shot of the second encounter with the for loop

If you would note in the 'Autos' tab at the bottom that all the data and the node itself's address is NULL - yet in the next screen shot it continues alt text

The program continues!!! what?>!

I pushed F-10 (the 'go to next command' button) ... and it jumps right over the statement? why? alt text

like image 846
Wallter Avatar asked Nov 16 '10 23:11

Wallter


3 Answers

0xcdcdcdcd is not a NULL pointer - that value is used in the debug builds of MSVC for memory that has been allocated but not initialized.

See When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete? for more details.

The root of your problem might be in the constructor that takes an int parameter:

Node (int dat) { Node(); data = dat; } 

The Node(); statement ends up doing nothing. This constructor leaves most of the members of the structure uninitialized.

like image 127
Michael Burr Avatar answered Oct 17 '22 21:10

Michael Burr


tmpNode is not null in any screenshot.

It's first 0x00294820, then 0xcdcdcdcd. The second is the magic debug value for uninitialized malloced memory.

like image 34
Matthew Flaschen Avatar answered Oct 17 '22 20:10

Matthew Flaschen


NULL, in C++, tends to be (but is not guaranteed to be) 0.

In your second/third screenshots, tmpNode = 0xcdcdcdcd, which is not NULL. 0xcdcdcdcd is the value Visual Studio gives to uninitialized variables (when running a debug release).

Make sure to initialize all all your nodes' fields:

Node* root = NULL;
or
Node* root = new Node(); //Don't forget to delete!

Setting fields to NULL is not done automatically in C++ as it is in other languages like Java and C#.

like image 1
BlueRaja - Danny Pflughoeft Avatar answered Oct 17 '22 22:10

BlueRaja - Danny Pflughoeft