Visual Studio 2010 调试“if (var == NULL)”不触发
已解决 - 构造函数
Matthew Flaschen 和 Michael 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
};
导致问题的搜索功能
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
}
}
}
第一次遇到 for 循环
第二次遇到for 循环的屏幕截图
如果您在底部的“自动”选项卡中注意到所有数据和节点本身的地址都是 NULL
- 但在下一个屏幕截图中它会继续
节目继续!!!什么?>!
我按下 F-10(“转到下一个命令”按钮)...它会直接跳过该语句?为什么?
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
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
The program continues!!! what?>!
I pushed F-10 (the 'go to next command' button) ... and it jumps right over the statement? why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
0xcdcdcdcd
不是 NULL 指针 - 该值在 MSVC 的调试版本中用于已分配但未初始化的内存。请参阅操作系统何时以及为什么会在 malloc/free/new/delete 上将内存初始化为 0xCD、0xDD 等? 了解更多详细信息。
问题的根源可能在于采用
int
参数的构造函数:Node();
语句最终什么也不做。此构造函数使结构的大多数成员未初始化。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:The
Node();
statement ends up doing nothing. This constructor leaves most of the members of the structure uninitialized.在任何屏幕截图中,
tmpNode
都不为 null。首先是
0x00294820
,然后是0xcdcdcdcd
。第二个是未初始化的magic调试值>分配内存。
tmpNode
is not null in any screenshot.It's first
0x00294820
, then0xcdcdcdcd
. The second is the magic debug value for uninitializedmalloc
ed memory.在 C++ 中,
NULL
往往是(但不保证是)0
。在您的第二个/第三个屏幕截图中,
tmpNode = 0xcdcdcdcd
,它不是NULL
。0xcdcdcdcd
是 Visual Studio 赋予未初始化变量的值(运行调试版本时)。确保初始化所有节点的字段:
在 C++ 中将字段设置为 NULL 不会像在 Java 和 C# 等其他语言中那样自动完成。
NULL
, in C++, tends to be (but is not guaranteed to be)0
.In your second/third screenshots,
tmpNode = 0xcdcdcdcd
, which is notNULL
.0xcdcdcdcd
is the value Visual Studio gives to uninitialized variables (when running a debug release).Make sure to initialize all all your nodes' fields:
Setting fields to NULL is not done automatically in C++ as it is in other languages like Java and C#.
tmpNode
引用未初始化的内存,通常不保证为 null。例如,以下语句不保证 tmpNode 为 null。您将
tmpNode
分配给root
,我怀疑root
未初始化,因此tmpNode
的值为非空值。请检查您对root
的初始化 - 我无法对其发表评论,因为您尚未发布此特定代码。tmpNode
is referencing uninitialized memory, which is generally not guaranteed to be null. For instance, the following statement does not guarantee thattmpNode
is null.You are assigning
tmpNode
toroot
and I suspect thatroot
is uninitialized, hence the non-null value oftmpNode
. Please check your initialization ofroot
-- I cannot comment on it as you haven't posted this specific code.