简体   繁体   English

在查找错误时需要帮助(与指向堆栈上变量的指针有关)

[英]Need help in finding the bug (related to pointers to variable on stack)

Can some one please tell me what is wrong in my program. 有人可以告诉我程序中有什么问题吗? This is basically a program to insert a new node in a binary search tree. 基本上,这是一个在二进制搜索树中插入新节点的程序。

The thing is that my insert function is working correctly and the node is getting inserted which i am verifying in the main program by the line 事实是我的插入功能正常工作,并且正在插入节点,这是我在主程序中通过行验证的

     cout<<a.left->right->right->data;

The output of which is coming correctly ie 5 输出正确,即5

but when i try to print a level order traversal of the binary tree some junk value is getting passed in place of the new node and the program is crashing 但是,当我尝试打印二进制树的级别顺序遍历时,某些垃圾值已被传递来代替新节点,并且程序崩溃了

Can some one please have a look and explain to me what i am doing wrong and how can in the main program the correct value is getting displayed. 可以请一个人看看并向我解释我做错了什么,以及如何在主程序中显示正确的值。

#include<iostream>
#include<string>
#include<conio.h>
#include<array>
#include<stack>
#include<sstream>
#include<algorithm>
#include<vector>
#include<ctype.h>//isdigit
#include<deque>
#include<queue>
#include<map>
using namespace::std;
struct BST
{
    int data;
    BST *left;
    BST *right;
    BST(int d,struct BST* l,BST *r):data(d) , left(l) ,right(r)
    {
    }
};

void levelOrder(struct BST *root)
{
    struct BST *temp=NULL;
    int count =0;
    deque<struct BST*> dq;
    if(!root)
    {
        return;
    }
    dq.push_back(root);
    count=dq.size();
    while(!dq.empty())
    {
        temp=dq.front();
        cout<<temp->data<<" ";
        if(temp->left)
        {
            dq.push_back(temp->left);
        }
        if(temp->right)
        {
            dq.push_back(temp->right);
        }
        dq.pop_front();
        if(--count==0)
        {
            cout<<endl;
            count=dq.size();
        }
    }
}
void Insert(struct BST*root,int data)
{
    struct BST temp(data,NULL,NULL);
    if(!root)
    {
        return;
    }
    while(root)
    {
        if((root)->data >data)
        {
            (root)=(root)->left;
            if(!(root)->left)
            {
                (root)->left=&temp;
                break;
            }
        }
        else
        {
            (root)=(root)->right;
            if(!(root)->right)
            {
                (root)->right=&temp;
                break;
            }
        }
    }
}
int main()
{
    deque<struct BST> dq1,dq2;
    BST e(4,NULL,NULL);
    BST f(3,NULL,NULL);
    BST d(1,&f,NULL);
    //BST g(4,NULL,NULL);
    BST b(2,&d,&e);
    BST c(8,NULL,NULL);
    BST a(6,&b,&c);
    levelOrder(&a);
    Insert(&a,5);
    cout<<a.left->right->right->data;
    cout<<endl;
    levelOrder(&a);
    _getch();
    return 0;
}

The reason is because you are putting in pointers to a local variable in the tree in the Insert function. 原因是因为您要在Insert函数的树中放入指向局部变量的指针。

When a function returns, all its local variables are no longer "alive", and accessing a pointer to one of those local variables is undefined behavior . 当函数返回时,其所有局部变量不再“活动”,并且访问这些局部变量之一的指针是未定义的行为 In fact, the memory those variables once occupied may be overwritten by the next function call, no matter what function you call. 实际上,无论您调用哪个函数,这些变量一旦被占用的内存都可能被下一个函数调用覆盖。

If you want to add a new node, you need to allocate it on the heap using eg new . 如果要添加新节点,则需要使用例如new在堆上分配它。 However, due to your design of the tree, this will cause a memory leak as you don't free any sub-nodes. 但是,由于树的设计,这将导致内存泄漏,因为您没有释放任何子节点。 In fact, as you use pointers to local variables in the main function (that's okay, as the lifetime of those variables are for the duration of main which is the whole program) you can't just simply delete the pointers Willy-nilly. 实际上,当您在main函数中使用指向局部变量的指针时(没关系,因为这些变量的生命周期是在main的整个程序中),您不能简单地delete Willy-nilly指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM