Home > Enterprise >  Fix segmentation fault
Fix segmentation fault

Time:01-24

I am trying to make a postfix to infix program using two stacks. The basic idea is this -

ptr1 --> ptr2 --> ptr3 --> ptr4 --> NULL

| --------- | -------- | --------- |

(4*5) ----5 -------- 6 -------- 10

Each operand is a separate stack and the ptr stack is for the memory address of first element of each operand. But I am getting a segmentation error. I think it is due to the (*ptr) = (*ptr)->next1; statement in the pop function. Can someone explain/fix this to make a pop function that returns the address of the operand stack at the top and pops the ptr stack at the same time? Complete code -

struct bracket {
  char c;
  struct bracket* next;
};

struct outer {
    struct bracket *down;
    struct outer *next1;
};
struct outer* push_ptr (struct bracket *ptr,struct outer * out){
    struct outer *temp = NULL;
    temp = (struct outer*)malloc(sizeof(struct outer));
    temp->down = ptr;
    temp->next1 = out;
    return temp;
}

struct bracket* push_ch(char ch,struct bracket *head){

  struct bracket *temp = NULL;
  temp = (struct bracket*)malloc(sizeof(struct bracket));
  temp->c = ch;
  temp->next = head;
  return temp;
}

struct bracket* pop(struct outer **ptr){
  struct bracket *top = (struct bracket*)malloc(sizeof(struct bracket));
  top = (*ptr)->down;
  (*ptr) = (*ptr)->next1;
  return top;
}

int main(){

    char c[] = "25,9,6,/,-,3,/";
    struct outer * address = NULL;
    struct bracket * num;
    for (int i=0;i<strlen(c);i=i 1){
        
        if(c[i]==',') continue;
        
        else if (c[i]=='*' || c[i]=='-' || c[i]==' ' || c[i]=='/'){
            struct bracket *top1 = pop(&address);
            struct bracket *top2 = pop(&address);
            
        } 
        
        else {
               num = NULL;
               while(c[i]!=','){
                   num = push_ch(c[i],num);
                   i = i 1;
               }               
               address = push_ptr(num,address);
               
        }

    }
    return 0;

}

CodePudding user response:

Your problem is definitely in the function pop()...

struct bracket* pop(struct outer **ptr){
  struct bracket *top = (struct bracket*)malloc(sizeof(struct bracket));
  top = (*ptr)->down;
  (*ptr) = (*ptr)->next1;
  return top;
}

You are poping from the outer ptr every time and eventually, at some point, it will remain empty. Let us understand what will happen when it is empty. So you pass a reference struct outer **ptr which points to NULL. Hence it will not be able to retrieve (*ptr)->down on this line ...

top = (*ptr)->down;

And You will have a segmentation fault. You should treat the NULL case in your pop function (in general it is good to NULL protect every function). An alternate implementation of the pop function could be this...

struct bracket* pop(struct outer **ptr){
  struct bracket *top = (struct bracket*)malloc(sizeof(struct bracket));
  if (*ptr)
  {
      top = (*ptr)->down;
      (*ptr) = (*ptr)->next1;
  }
  else
      top = NULL;
  return top;
}

This will fix the segmentation fault. It will return NULL if the outer struct contains no elements.

  •  Tags:  
  • Related