加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 函数中return语句处的Seg Fault

发布时间:2020-12-16 07:01:43 所属栏目:百科 来源:网络整理
导读:我的程序应该将提示从中缀转换为后缀.到目前为止,通过调试器和其他各种方法,我找到了我的段错误的确切点,但不明白为什么. 这是我的代码: 这是itop.h: using namespace std;#include cstdlib#include iostreamclass sNode{ public: char data; sNode *next;
我的程序应该将提示从中缀转换为后缀.到目前为止,通过调试器和其他各种方法,我找到了我的段错误的确切点,但不明白为什么.

这是我的代码:

这是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之前,我得到“Segmentation fault(core dumped)”错误消息.我使用了gdb,所有我知道的就是在“检查是否 – 或”之后我看到“$1 = 2”.我不太清楚这意味着什么,但这是我想要回归的.

谢谢您的帮助.

解决方法

修复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:修复其余的代码逻辑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读