Why am I getting SIG SEGV error?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct stack
{
  int size;
  int top;
  char *s;
};

int isOperand(char x)
{
 if(x=='+' || x=='-' || x=='*' || x=='/' || x=='^' || x=='(' || x==')' )
   return 0;

   return 1;
}

int inStackpre(char x)
{
 if(x=='+' || x=='-')
   return 2;
 if(x=='*' || x=='/')
   return 4;
 if(x=='^')
   return 5;
 if(x=='(' )
   return 0;

}


int outStackpre(char x)
{
  if(x=='+' || x=='-')
   return 1;
  if(x=='*' || x=='/')
   return 3;
  if(x=='^')
   return 6;
  if(x=='(' )
   return 7;
  if(x==')' )
   return 0;

}

void push(struct stack *st, char x)
{
 st->top++;
 st->s[st->top]=x;
}

char pop(struct stack *st)
{
 char x=st->s[st->top];
 st->top--;
 return x;
}


void convert(char *infix,int n)
{
 char postfix[n];
 char c;
 struct stack st;
 int i=0,j=0;
 st.size=n;
 st.top=-1;
 st.s=(char *)calloc(n ,sizeof(char));
 while(infix[i]!='\0')
 {
 if(isOperand(infix[i]))
  postfix[j++]=infix[i++];
 else
  {
   if(st.top==-1 || inStackpre(st.s[st.top]) < outStackpre(infix[i]))
      push(&st,infix[i++]);
   else
     {
        c=pop(&st);
        if(c!='(' )
         postfix[j++]=c;
        else
         i++;
     }
  }
   
 }

  while(st.top!=-1)
  {
    postfix[j++]=pop(&st);
  }

  for(int i=0;postfix[i]!='\0';i++)
  {
    printf("%c",postfix[i]);
  }
 free(postfix);
}


int main()
{
 int t,n;
 scanf("%d",&t);
 char *infix;
 for(int i=1;i<=t;i++)
 {
  scanf("%d",&n);
  infix=(char *) calloc(n,sizeof(char));
  scanf("%s",infix);
  convert(infix,n);
  free(infix);
 }

 return 0;
}

This crashes on the sample testcase.

You can’t free what you didn’t malloc.

2 Likes

Why is that so?Calloc can be freed.
I searched it on google

Fine - you can’t free what you didn’t malloc, calloc, or realloc :stuck_out_tongue:

2 Likes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct stack
{
  int size;
  int top;
  char *s;
};

int isOperand(char x)
{
 if(x=='+' || x=='-' || x=='*' || x=='/' || x=='^' || x=='(' || x==')' )
   return 0;

   return 1;
}

int inStackpre(char x)
{
 if(x=='+' || x=='-')
   return 2;
 if(x=='*' || x=='/')
   return 4;
 if(x=='^')
   return 5;
 if(x=='(' )
   return 0;
 return -1;
}


int outStackpre(char x)
{
  if(x=='+' || x=='-')
   return 1;
  if(x=='*' || x=='/')
   return 3;
  if(x=='^')
   return 6;
  if(x=='(' )
   return 7;
  if(x==')' )
   return 0;
   return -2;
}

void push(struct stack *st, char x)
{
 st->top++;
 st->s[st->top]=x;
}

char pop(struct stack *st)
{
 char x=st->s[st->top];
 st->top--;
 return x;
}


void convert(char *infix,int n)
{
 char postfix[n];
 char c;
 struct stack st;
 int i=0,j=0;
 st.size=n;
 st.top=-1;
 st.s=(char *)malloc(n*sizeof(char));
 while(infix[i]!='\0')
 {
 if(isOperand(infix[i]))
  postfix[j++]=infix[i++];
 else
  {
   if(st.top==-1 || inStackpre(st.s[st.top]) < outStackpre(infix[i]))
      push(&st,infix[i++]);
   else
     {
        c=pop(&st);
        if(c!='(' )
         postfix[j++]=c;
        else
         i++;
     }
  }
 }

  while(st.top>=0)
  {
    postfix[j++]=pop(&st);
  }
 postfix[j]='\0';
  printf("%s\n",postfix);
 free(postfix);
}


int main()
{
 int n,t;
 scanf("%d",&t);
 char *infix;
 for(int i=1;i<=t;i++)
 {
  scanf("%d",&n);
  infix=(char *) malloc(n*sizeof(char));
  scanf("%s",infix);
  convert(infix,n);
  free(infix);
 }

 return 0;
}

Why is this code showing SIGABRT error?

But I did use calloc…So what is the problem with free?