空指针问题?
void Insert(AVLnode * & root, string X)
{
if ( root == NULL)
{
root = GetNode(X);
}
else if ( root->info > X )
{
Insert(root->left,X);
if ( height(root->left) - height(root->right) == 2 )
if ( X < root->left->info )
RotateRR(root);
else
RotateLR(root);
}
else if ( root->info < X )
{
Insert(root->right,X);
if ( height(root->right) - height(root->left) == 2 )
if ( X > root->right->info )
RotateRR(root);
else
RotateLR(root);
}
root->height = max( height(root->left), height(root->right) )+1;
}
AVLnode* find(AVLnode* root,string X)
{
if (!root) return 0;
if ( root->info == X )
return root;
else if (root->info < X)
find(root->left,X);
else if (root->info > X)
find(root->right,X);
return 0;
}
int main(int argc,char* argv)
{
AVLnode* Dic;
Insert(Dic,"adf");
return 0;
}
第一次插入时,root
为 NULL,但当我调试时,它会跳过 root == null
。这是怎么回事?
void Insert(AVLnode * & root, string X)
{
if ( root == NULL)
{
root = GetNode(X);
}
else if ( root->info > X )
{
Insert(root->left,X);
if ( height(root->left) - height(root->right) == 2 )
if ( X < root->left->info )
RotateRR(root);
else
RotateLR(root);
}
else if ( root->info < X )
{
Insert(root->right,X);
if ( height(root->right) - height(root->left) == 2 )
if ( X > root->right->info )
RotateRR(root);
else
RotateLR(root);
}
root->height = max( height(root->left), height(root->right) )+1;
}
AVLnode* find(AVLnode* root,string X)
{
if (!root) return 0;
if ( root->info == X )
return root;
else if (root->info < X)
find(root->left,X);
else if (root->info > X)
find(root->right,X);
return 0;
}
int main(int argc,char* argv)
{
AVLnode* Dic;
Insert(Dic,"adf");
return 0;
}
The first time in Insert
, root
is NULL but when I debug, it skips root == null
. What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在
main()
的AVLnode* Dic;
语句中。您正在从main()
发送一个未初始化的指针到insert()
。它包含垃圾值。将其初始化为NULL
The problem is in the
AVLnode* Dic;
statement ofmain()
. You are sending an uninitialized pointer toinsert()
frommain()
. It contains garbage values. Initialize it toNULL
第一次它不为NULL。在 C++ 中变量不会自动初始化为 NULL 或 0;它们包含垃圾(无论它们之前占用的内存地址是什么)。
将
AVLnode* Dic;
替换为AVLnode* Dic = NULL;
,然后重试。The first time it's not NULL. Variables do not auto-initialize to NULL or 0 in C++; they contain garbage (whatever the memory address they occupy contained before).
Replace
AVLnode* Dic;
withAVLnode* Dic = NULL;
and try again.“第一次插入”,在您给出的代码中,
root
是一个“随机”值,因为您从未初始化Dic
- 所以它很可能会发生当您在没有调试器的情况下运行时,或者如果您正在使用调试器时,则为 NULL。这只是代码中的一个明确错误,因此请在main
正文第一行中的;
之前添加= 0
。之后,您可能会发现更多错误(很难说,因为您没有向我们展示GetNode
或Rotate
东西,即使您这样做了 向我们展示从未被调用过的find
函数——确实是向我们展示了特殊的代码选择)。"The first time in Insert", in the code you give,
root
is a "random"-ish value, because you never initializeDic
-- so it may well happen to be NULL when you're running without a debugger and otherwise if you are using a debugger. That's just a definite bug in your code, so add= 0
before the;
in the first line ofmain
's body. After which, you may find more bugs (hard to say, since you don't show usGetNode
or theRotate
thingies, even though you do show us thefind
function that's never ever called -- peculiar choice of code to show us, indeed).