简体   繁体   English

奇怪的细分错误

[英]Weird segmentation fault errors

Ok, so im working on an assignment and for the life of me I cannot figure out why I am getting these segmentation faults. 好的,所以我正在从事一项任务,为了我的一生,我无法弄清楚为什么我会遇到这些细分错误。 Im still in the process of learning c++, programming in general, so I was hoping someone wiser than me can help me out. 我总体上仍在学习c ++和编程的过程,所以我希望有人比我有智慧,可以帮助我。 The program is a self organizing binary search tree and I wasn't having too much difficulty with it until now. 该程序是一个自组织的二进制搜索树,到目前为止,我并没有遇到太多困难。 Here is the beginning of a main program I am using to test my class BST, I cannot alter the mian program since it is an assignment. 这是我用来测试班级BST的主程序的开始,由于它是一项作业,因此我无法更改主程序。

int main() {
string input;

// get a list of integer values
cout << "Enter a list of integer values in one line: ";
getline(cin, input);

cout << "\n**CHECKPOINT 1**\n";

// create a binary search tree
BST<int> bst1(input);


if (!bst1.empty()) {
    cout << "\n**CHECKPOINT 2**\n";
    cout << "Inorder traversal: ";
    bst1.printInOrder();
    cout << "Level order traversal: ";
bst1.printLevelOrder();

I have yet to get past the printInOrder() function, here is the code for that 我还没有通过printInOrder()函数,这是该代码

template <typename T>
void BST<T>::printInOrder(BSTNode* t) const
{
    if (t->left != NULL)
        printInOrder(t->left);
    std::cout << " " << t->data << " ";
    if (t->right != NULL)
        printInOrder(t->right); 
}

The really strange thing that is confusing me if that when I add a quick cout<< "Something" to the first line of the printInOrder function, it all of a suddent will the print line 如果我在printInOrder函数的第一行中添加一个快速cout <<“ Something”时,突然之间将出现打印行,那真是让我感到困惑的事情

cout << "Inorder traversal: ";

and it will also start printing some of the numbers in the tree before finally giving me a segmentation fault again. 并且在最终再次给我分段错误之前,它还将开始在树中打印一些数字。 :/ :/

So, I would be really grateful if someone could explain to me WTF is going on. 因此,如果有人可以向我解释WTF正在进行,我将非常感谢。 The adding or subtracting of a simple cout line shouldn't change things like that should it? 添加或减去简单的提示行不应该改变那样的事情吗? Also, I feel like there are better ways of debugging this, if anyone has techniques that they use to figure this stuff out, please share :) Thanks in advance! 另外,我觉得有更好的方法调试此问题,如果有人掌握了解决问题的技巧,请分享:)预先感谢!

EDIT: I have tried the debugger GDB, I was unable to figure it out, but then again im not very well versed in the advanced features of debuggers so I might have missed something. 编辑:我已经尝试了调试器GDB,我无法弄清楚,但是我还是不太精通调试器的高级功能,所以我可能错过了一些东西。 The only other function that is even run, is the constructor to build from the string input. 甚至可以运行的唯一其他函数是从字符串输入生成的构造函数。 From what I could tell from the debugger is that the constructor seems to be working fine but nonetheless here's the code 从调试器可以看出的是,构造函数似乎工作正常,但是这里的代码

template <typename T>
BST<T>::BST(const std::string input, int th)
{
    threshold = th;
    root = NULL;        
    T v;
    // Make Input String Stream for easy use of >> operator
    std::istringstream iss (input);
    do
    {
        iss >> v;
        insert(v, root);
    }while(iss.good());
}

EDIT2: 编辑2:

Here is the code for my insert function, Thanks for the help everybody! 这是我插入函数的代码,谢谢大家的帮助! :) :)

template <typename T>
void BST<T>::insert(const T& v, BSTNode *&t)
{
    if(t == NULL)
        {
            t = new BSTNode;
            t->left = NULL;
            t->right = NULL;
            t->data = v;
            t->searchCount = 0;
        }
    else if( v < t->data )
        insert(v, t->left);
    else
        insert(v, t->right);
}

There's a marked lack of newlines in your output. 您的输出中明显缺少换行符。 Often the line buffering means you don't see anything until a newline is encountered. 行缓冲通常意味着您在遇到换行符之前不会看到任何内容。

I'd modify the line after the PrintOnOrder to this:- 我将在PrintOnOrder之后的行修改为:-

    cout << "\nLevel order traversal: ";

In the constructor you are inserting v into the tree even if reading the data with iss >> v failed. 在构造函数中,即使将iss >> v的数据读取失败,也要将v插入树中。 Probably you rather want something like this: 也许您宁愿这样的事情:

while (iss >> v) {
   insert(v, root);
}

But probably the real cause for your segmentation fault lies in insert() , for example if that function just inserts a pointer to the (stack allocated) parameter it receives into the tree. 但是,造成分段错误的真正原因可能在于insert() ,例如,如果该函数只是将指向其接收的(堆栈分配的)参数的指针插入树中。 The parameter variable will go out of scope at the end of the function (and therefore cease to exist). 参数变量将在函数末尾超出范围(因此不再存在)。 If you just stored a pointer to that variable into the tree, that pointer will no longer point to anything useful. 如果只是将指向该变量的指针存储到树中,则该指针将不再指向任何有用的东西。

I don't see anything wrong here. 我在这里没有发现任何问题。 As others pointed out, output buffering may mean that your code actually completes printInOrder() successfully and then crashes somewhere later. 正如其他人指出的那样,输出缓冲可能意味着您的代码实际上成功完成了printInOrder(),然后在以后崩溃。

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

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