简体   繁体   English

分段错误 - 核心转储

[英]Segmentation Fault - Core dumped

This is my program to convert an expression from postfix to infix using stacks.这是我使用堆栈将表达式从后缀转换为中缀的程序。 I am getting no errors but on running, the compiler says Segmentation Fault - Core Dumped.我没有收到任何错误,但在运行时,编译器说 Segmentation Fault - Core Dumped。 I think it has something to do with a pointer pointing to a garbage value.我认为这与指向垃圾值的指针有关。 How do I tracebck to this rogue pointer?我如何追踪到这个流氓指针? Also, is there any better method to convert postfix to infix?另外,有没有更好的方法将后缀转换为中缀?

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
class StringStack
{
    public:
    char arr[20][20];
    int top;
    StringStack()
    {top=-1; arr[20][20]={NULL};}
    void push(char* a)
    {strcpy(arr[++top],a);}
    char* pop()
    {return arr[top--];}
};
void paranthesise(char a[])
{
    int l = strlen(a);
    a[l+2] = '\0';
    for(int i=l; i>=0; i++)
        a[i] = a[i-1];
    a[0] = '(';
    a[l+1] = ')';

}
using namespace std;
int main()
{
    char temp[1];
    char postfix[20] = "";
    char temp2[10] = "";
    char temp3[10] = "";
    StringStack s1;
    cout<<"Enter Postfix Expression: "<<endl;
    gets(postfix); int l = strlen(postfix);
    for(int i=0; i<l;   i++)
    {
        if(postfix[i]!='*'&&postfix[i]!='/'&&postfix[i]!='+'&&postfix[i]!='-')
        {
            temp[0]=postfix[i];
            s1.push(temp);
        }
        else
        {
            strcpy(temp2,s1.pop());
            strcpy(temp3,s1.pop());
            switch(postfix[i])
            {
                case'*':
                    temp[0]='*'; break;
                case'/':
                    temp[0]='/'; break;
                case'+':
                    temp[0]='+'; break;
                case'-':
                    temp[0]='-'; break;

                default: cout<<"Error"; break;
            }
            strcat(temp3,temp);
            strcat(temp3,temp2);
            paranthesise(temp3);
            s1.push(temp3);
        }
    }
    strcpy(temp2,s1.pop());
    cout<<"\nInfix:";
    puts(temp2); 
    return 0;
}

Problems that I found:我发现的问题:

One

arr[20][20]={NULL};

is wrong.是错的。 This assigns NULL to arr[20][20] , which is not a valid element of the array.这将NULL分配给arr[20][20] ,它不是数组的有效元素。 You are setting the value of memory that you are not supposed to.您正在设置不应该设置的内存值。 Valid elements of arr are arr[0][0] - arr[19][19] . arr有效元素是arr[0][0] - arr[19][19]

That in itself is sufficient for the program to exhibit undefined behavior.这本身就足以让程序表现出未定义的行为。

I would change the constructor to:我会将构造函数更改为:

StringStack() : arr{}, top(-1) {}

Two

When you use当你使用

char temp[1];

the only valid string it can hold is the empty string.它可以保存的唯一有效字符串是空字符串。 Since you mean to use it hold a string consisting of one character, you need to use:由于您打算使用它来保存一个由一个字符组成的字符串,因此您需要使用:

char temp[2] = ""; // Doesn't have to be 2 but it has to be 
                   // greater than 1.

Three

In paranthesise , you have:paranthesise ,你有:

for(int i=l; i>=0; i++)

That needs to be:那需要是:

for(int i=l; i>=0; i--)

Otherwise, you keep on modifying the array without ever meeting the criterion to end the loop.否则,您会继续修改数组,而不会满足结束循环的条件。 Not only that, you also end up modifying the array beyond the valid limits.不仅如此,您最终还会修改超出有效限制的数组。

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

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