-5

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. 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;
}
  • 1
    Possible duplicates: [#1](http://stackoverflow.com/q/20612172), [#2](http://stackoverflow.com/q/22546079), [#3](http://stackoverflow.com/q/4363133). – Kerrek SB Sep 14 '15 at 01:00
  • In `paranthesise`: `for (int i = l; i >= 0; i++)` I'm guessing you wanted `i--` here. – user657267 Sep 14 '15 at 01:01
  • 1
    *"Also, is there any better method to convert postfix to infix?"* Too broad. – Baum mit Augen Sep 14 '15 at 01:01
  • 3
    "on running, the compiler says" - No it doesn't. The compiler is the part of C++ which happens _before_ running. While running, the OS is in charge, and the C++ runtime library is in support. A SegFault is an OS message, usually caused by bugs in your code. – MSalters Sep 14 '15 at 01:14

1 Answers1

2

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. You are setting the value of memory that you are not supposed to. Valid elements of arr are 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:

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270