0

I tried to implement to stack in C. Here is my code, I ran the code, the code ended with:

Thread 1: EXC_BAD_ACCESS error

I am so confused, don't know what went wrong, can anyone debug y code? Thanks!

And I have one more question, why command+k key didn't work for me? I have to indent line by line.

#include <stdio.h>
#include <stdlib.h>   
#define init_size 10
#define increment 1

typedef struct sqStack
{
    int* top;
    int* base;
    int stack_size;
} sqStack;

int init_stack(sqStack* sq)
{
    if(sq->base==NULL)
    {
        sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS 
    }
    if(sq->base==NULL) exit(-1);
    sq->stack_size=init_size;
    sq->top=sq->base;
    return 1;
}

int push(sqStack* sq, int e)
{
    if(sq==NULL) exit(-1);
    if(sq->top-sq->base==sq->stack_size)
    {
        int* q = (int*)realloc(sq->base,  
        (init_size+increment)*sizeof(int));
        if(q==NULL) exit(-1);
        else
        {
            sq->base=q;
            sq->stack_size += increment;
            sq->top=sq->base+sq->stack_size;
        }
        *sq->top++=e;
    }
    return 1;
}

int pop(sqStack* sq,int*e)
{
    if(sq==NULL) exit(-1);  
    if(sq->base==sq->top)  exit(-1);   
    sq->top-=1;   
    *e=*sq->top;   
    return 1;    
}

int empty(sqStack* sq)
{
    if(sq->base==sq->top) return 1;
    else return 0;
}


int main()
{
    sqStack* sq=NULL;
    init_stack(sq);
    push(sq,1);
    int *e=(int*)malloc(sizeof(int));
    pop(sq,e);
    printf("%d\n",*e);
    return 0;
}
2
  • You are passing a NULL pointer to init_stack where you try to dereference it. Commented Jan 27, 2019 at 16:54
  • how should I fix it then? Commented Jan 27, 2019 at 17:02

1 Answer 1

1
  1. In function int init_stack(sqStack* sq) the
 if(sq->base==NULL)
   {
     sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS 
   }

should be:

sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS 
  1. In function int push(sqStack* sq, int e)

the

    }
     *sq->top++=e;
}
return 1;
}

should be

    }
  }
  *sq->top++=e;
  return 1;
}
  1. In function int main()

the is not need use int *e=(int*)malloc(sizeof(int)); just use int e;

the

sqStack* sq=NULL;
init_stack(sq);
push(sq,1);
int *e=(int*)malloc(sizeof(int));
pop(sq,e);
printf("%d\n",*e);

should be

sqStack sq;
init_stack(&sq);
push(&sq,1);
int e;
pop(sq,&e);
printf("%d\n",e);

The following code could work:

#include <stdio.h>
#include <stdlib.h>
#define init_size 10
#define increment 1

typedef struct sqStack {
  int* top;
  int* base;
  int stack_size;
} sqStack;

int init_stack(sqStack* sq) {
  sq->base = (int*)malloc(init_size * sizeof(int));
  if (sq->base == NULL) exit(-1);
  sq->stack_size = init_size;
  sq->top = sq->base;
  return 1;
}

int push(sqStack* sq, int e) {
  if (sq == NULL) exit(-1);
  if (sq->top - sq->base == sq->stack_size) {
    int* q = (int*)realloc(sq->base, (init_size + increment) * sizeof(int));
    if (q == NULL)
      exit(-1);
    else {
      sq->base = q;
      sq->stack_size += increment;
      sq->top = sq->base + sq->stack_size;
    }
  }
  *sq->top++ = e;
  return 1;
}

int pop(sqStack* sq, int* e) {
  if (sq == NULL) exit(-1);
  if (sq->base == sq->top) exit(-1);
  sq->top -= 1;
  *e = *sq->top;
  return 1;
}

int empty(sqStack* sq) {
  if (sq->base == sq->top)
    return 1;
  else
    return 0;
}

int main() {
  sqStack sq;
  init_stack(&sq);
  push(&sq, 1);
  int e;
  pop(&sq, &e);
  printf("%d\n", e);
  return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Before use a pointer, you must init like sqStack* sq=&stack; not sqStack* sq=NULL; @Liu
what is the difference between sqStack* sq and sqStack &sq?? Why the later works? And isn't sqStack &sq=NULL too??
sqStack& sq; is reference use in c++, sqStack* sq; is pointer use in c and c++, sqStack* sq=NULL; sq point NULL, sqStack stack; sqStack* sq = &stack; sq point stack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.