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

Solved - problem with constructor

Matthew Flaschen and Michael Burr pointed out a problem of an overloaded Node(int) constructor calling Node() , which does not work because ... Thanks guys!


I created a program (I am debugging it) and faced with some strange problem ... the operation `if` does not start when it should be ... This is a school project in which we must build an AVL Tree with at least one" optimizing "function.

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

I hope this is enough to solve this strange problem ... I have never encountered anything similar 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 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 }; 

Problem Finder

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

Screenshot of the first meeting with for loop

alt text

Screenshot of the second meeting with for loop

If you mark on the β€œAuto” tab at the bottom that all the data and the node address itself are NULL , but in the next screenshot it continues alt text

The program continues !!! what? >

I pressed the F-10 button (the "go to the next command" button) ... and it jumps right over the expression? What for? alt text

+6
c ++ null if-statement visual-studio-2010
source share
4 answers

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

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

The root of your problem may be in the constructor, which takes an int parameter:

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

Operator Node(); does nothing. This constructor leaves most members of the structure uninitialized.

+4
source share

tmpNode not null in any screenshot.

First 0x00294820 , then 0xcdcdcdcd . The second is the magic debug value for uninitialized malloc memory.

+2
source share

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

In the second / third screenshots tmpNode = 0xcdcdcdcd , which is not NULL . 0xcdcdcdcd is the value that Visual Studio provides with uninitialized variables (when starting the debug version).

Be sure to initialize all the fields of your nodes:

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

Fields for NULL are not automatically executed in C ++, as in other languages ​​such as Java and C #.

+1
source share

tmpNode refers to uninitialized memory, which is usually not guaranteed to be null. For example, the following statement does not guarantee that tmpNode is null.

 Node* tmpNode; // or assignment to another uninitialized variable. 

You assign tmpNode to root , and I suspect that root initialized, therefore this value is not null tmpNode . Check your root initialization - I cannot comment on this since you have not posted this code.

0
source share

All Articles