在函数中返回语句中的seg故障

Seg Fault at return statement in function

本文关键字:seg 故障 返回 函数 语句      更新时间:2023-10-16

我的程序应该将提示从infix转换为后缀。到目前为止,通过调试器和其他各种方法,我找到了我所掌握的确切点,但不明白为什么。

这是我的代码:

这是itop.h:

using namespace std;
#include <cstdlib>
#include <iostream>
class sNode{
    public:
       char data;
       sNode *next;
};
class stack{
    public:
        sNode *head;
        void push (char);
        sNode pop();
        int rank(char);
        stack()
        {
        cout << "Initiliazing stack." << endl;
        }
};

这是我的itop.cpp文件:

    #include "itop.h"
    void stack::push (char a)
    {
            // cout << "Pushing " << a << endl;
            sNode *sn;
            sn = new sNode;
            sn->data = a;
            sn->next = head;
            head = sn;
    }
    sNode stack::pop()
    {
            // cout << "Popping stack." << endl;
            sNode *sn;
            sn = head;
            head = head->next;
            return *sn;
    }
    int stack::rank(char x)
    {
            int num = 0;
            // cout << "Checking rank." << endl;
            if(x == '')
            {
                    num = 1;
                    // cout << "Checking for null" << endl;
                    return num;
            }
            else if(x == '+' || x == '-')
            {
                    num = 2;
                    // cout << "Checking if + or -" << endl;
                    return num;
                    // cout << "After return." << endl;
            }
            else if(x == '*' || x == '/')
            {
                    num = 3;
                    // cout << "Checking for * or /" << endl;
                    return num;
            }
            else
                    cout << "Error! Input not valid!" << endl;
    }

这是main.cpp:

using namespace std;
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "itop.h"
int main()
{
char *temp1;            //Instantiating variables.
char *temp2;
temp1 = new char[20];
temp2 = new char [20];
stack s;
do              //Checking commands.
{
    cout << "infix_to_postfix> ";
    cin >> temp1;
    if(strcmp(temp1, "quit") == 0)
    {
        return 0;
    }
    if(strcmp(temp1, "convert") != 0)
    {
        cout << "Error! Invalid command." << endl;
    }
    cin >> temp2;
    if(strcmp(temp1, "convert") == 0)
    {
        for(int i=0; i<sizeof(temp2); i++)
        {
            if(isdigit(temp2[i]))
            {
                cout << atoi(&temp2[i]);
            }
            else if(s.rank(temp2[i]) < s.rank(s.head->data))
            {
                sNode temp = s.pop();
                cout << temp.data;
            }
            else
            {
                s.push(temp2[i]);
            }
        }
    }
    else
    {
        cout << "Error! Command not supported." << endl;
    }
}while(strcmp(temp1, "quit") != 0);
return 0;
}

该函数在

上调用
else if(s.rank(temp2[i]) < s.rank(s.head->data))

问题在这里:

            else if(x == '+' || x == '-')
            {
                    num = 2;
                    // cout << "Checking if + or -" << endl;
                    return num;
                    // cout << "After return." << endl;
            }

特别是在返回num之前,我会收到"分段故障(核心倾倒)"错误消息。我已经使用了GDB,我所知道的是,在"检查 或 - "我看到" $ 1 = 2"之后。我不太确定这是什么意思,但这是我要返回的。

谢谢您的帮助。

您的代码中有很多错误。您的堆栈实现是错误的。例如,push()仅一遍又一遍地设置head。这会导致您的堆栈类只能容纳一个元素。next永远不会设置为任何东西,因此它包含随机垃圾。在此外,您有:

for(int i=0; i<sizeof(temp2); i++)

sizeof(temp2)不会给您字符串temp2点的字符量。它为您提供指针temp2本身的大小。此外,您最终从一个空堆栈中读取s.head,这将是指向随机垃圾的指针。那时,所有赌注当然都没有。除了崩溃和燃烧外,您什么都不期望。

修复1:编写合适的构造函数。

    stack()
    {
    head=NULL;
    cout << "Initiliazing stack." << endl;
    } 

修复2:写一个额外的方法来检查堆栈是否为空。

int stack::empty()
{
    if(head == NULL)
      return true;
    else
      return false;
}

修复3:在使用堆栈数据之前检查是否堆叠为空。

else if(!s.empty() && s.rank(temp2[i]) < s.rank(s.head->data))
{
 ...
}

修复4:修复代码逻辑的其余部分。