【问题标题】:Seg Fault at function call函数调用时的段错误
【发布时间】:2025-12-19 15:30:06
【问题描述】:

我正在尝试编写一个对树执行不同功能的程序,到目前为止,除了打印功能之外,所有这些功能都可以工作。它以前可以工作,但是在尝试解决其他功能中的一些问题时(不要弄乱它),现在它们已修复,这个突然不起作用,我无法理解为什么。这是我的代码:

main.cpp:

using namespace std;
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "lcrs.h"

int main()
{
char *temp1;
char *temp2;
temp1 = new char;
temp2 = new char;

lcrs tree;

do{
    cout << "LCRS> ";
    cin >> temp1;
    if(strcmp(temp1, "quit") == 0)
    {
        return 0;
    }
    if(strcmp(temp1, "insert") == 0)
    {   cin >> temp2;
        bool error;
        for(int i=0; i<strlen(temp2); i++)
        {
            if(!isdigit(temp2[i]))
            {
                cout << "Error!" << endl;
                error = true;
            }
        }
        if(!error)
        {
            tree.insert(atoi(temp2), tree.root);
        }
    }
    else if(strcmp(temp1, "height") == 0)
    {
        if(tree.root == NULL)
            cout << "-1" << endl;
        else
            cout << tree.getHeight(tree.root) << endl;
    }
    else if(strcmp(temp1, "preorder") == 0)
    {
        cout << "Root is " << tree.root->data << endl;
        tree.print(tree.root);
        cout << "" << endl;
    }
    else if(strcmp(temp1, "search") == 0)
    {
        cin >> temp2;
                bool error;
                for(int i=0; i<strlen(temp2); i++)
             {
                       if(!isdigit(temp2[i]))
                       {
                                cout << "Error!" << endl;
                               error = true;
                     }
                }
               if(!error)
                   {
                         if(tree.search(atoi(temp2), tree.root))
                cout << "true" << endl;
            else
                cout << "false" << endl;
                }

    }
    else
    {
        cout << "Error! " << endl;
    }
}while(strcmp(temp1, "quit") !=0);

return 0;
}

lcrs.h:

using namespace std;
#include <cstdlib>
#include <iostream>

class node{
    public:
    int data;
    node *right;
    node *below;

    node()
    {
        right = NULL;
        below = NULL;
    }
};

class lcrs{
    public:
    node *root;
    bool search(int, node*);
    void print(node*);
    void insert(int, node*&);
    int getHeight(node*);

    lcrs()
    {
        root = NULL;
    }
};

lcrs.cpp:

using namespace std;
#include "lcrs.h"

bool lcrs::search(int x, node *b)
{
    if(b == NULL)
        return false;
    else
    {
        if(b->data == x)
            return true;
        else
        {
            return search(x, b->right) || search(x, b->below);
        }
    }
}

void lcrs::print(node *z)
{
    if(z->below == NULL || z->right != NULL)
    {
        cout << z->data << ",";
        print(z->right);
    }
    else if(z->below != NULL && z->right == NULL)
    {
        cout << z->data << ",";
        print(z->below);
    }
    else if(z->below != NULL && z->right != NULL)
    {
        cout << z->data << ",";
        print(z->below);
        print(z->right);
    }
    else if(z->right == NULL && z->below == NULL)
     {
             cout << z->data << "";
     }


}

void lcrs::insert(int x, node *&a)
{
    if(a == NULL)
    {
        node *newnode;
        newnode = new node;
        newnode->data = x;
        a = newnode;
    }
    else if(a->data < x)
    {
        if(a->right != NULL)
        {
            insert(x, a->right);
        }
        else if(a->below != NULL)
        {
            if(a->below->right != NULL)
            {
                insert(x, a->below->right);
            }
            else
            {
                insert(x, a->below);
            }
        }
        else
        {
            node *n;
            n = new node;
            n->data = x;
            a->below = n;
        }
    }
    else if(a->data > x)
    {
        if(a->below != NULL)
        {
            insert(x, a->below);
        }
        else
        {
            node *n;
            n = new node;
            n->data = x;
            a->right = n;
        }
    }
}
int lcrs::getHeight(node *h)
{
    int height = 0;
    node *n;
    n = new node;
    n = h;
    while(n->below != NULL || n->right != NULL)
    {
        if(n->below != NULL)
        {
            n = n->below;
            height ++;
        }
        else if(n->right != NULL)
        {
            n = n->right;
        }
    }
    return height;
}

我在 tree.print(tree.root) 函数调用时遇到了段错误。我在函数的开头放了一个打印语句,但它从来没有做到这一点,所以我有点困惑问题出在哪里。

非常感谢您的帮助。

【问题讨论】:

  • 听说过一种叫做调试的花哨的东西吗?
  • 你真的应该尝试将代码剥离到重现错误真正需要的地方。您自己更容易找到问题所在,我们也更容易阅读
  • 与您之前的版本进行比较,并找出您在“消除”这些问题时搞砸了什么。
  • 很可能您将一个 NULL 指针传递给某处的 print() 方法。您应该使用调试器来找出发生这种情况的位置(正如其他人已经推荐的那样)。

标签: c++ class segmentation-fault


【解决方案1】:

有很多问题。它可能与您读取输入的方式有关,将字符串填充到包含单个字符的缓冲区中(使用 std::string 代替 - 它的存在是有原因的)或者它可能有一些原因处理 tree.root 可能为 null 并且您正在取消引用它的事实:cout &lt;&lt; "Root is " &lt;&lt; tree.root-&gt;data &lt;&lt; endl;

此外,您确实应该在发布代码时尽量减少内容。这有两个目的:它可以帮助您(因为您实际上可能发现您自己出了什么问题,或者至少隔离了故障,因为您正在减少事情)并且它可以帮助我们,因为我们不'不需要浏览页面和代码页面。

【讨论】:

  • 我已经检查了根不是空的,使用你在那里的那个语句,它不是。而且我担心如果我只发布与问题相关的内容,我可能会遗漏一些外人可能会注意到的内容。
  • 检查 root 是否为空。您正在无条件地取消对 root 的引用,而且它很可能为空。
【解决方案2】:

我找到了问题,只是一个小错误。 (当然是。) 感谢所有给出合理答案并真诚尝试提供帮助的人。 还有,感谢所有给我sass的人。我很感激在我使用调试器并且仍然束手无策之后,人们可以提醒我我只是一个低级 ComSci 学生。非常感谢。

【讨论】:

  • 很好地找到了它。至于 sass,如果您觉得被轻视了,请见谅,但是对于每个使用过调试器的 Sarah Awesome,有 100 个非真棒的人没有使用过调试器。祝你的任务好运。还有一个小旁注:您可能需要重新检查您的“lcrs::print”,它有点过于复杂,可以简化并更容易理解(和调试)。
最近更新 更多