C stack implementation throws bad access error -


am building stack in c. stack fails exc_bad_access error on if check in isempty() function, , i'm not sure why.

typedefs & definitions:

#define nodesize sizeof(stacknode)  struct stacknode {     int data;     struct stacknode *nextptr; };  typedef struct stacknode stacknode; typedef stacknode *stacknodeptr; 

stack operation functions

void push(stacknodeptr *topptr, int value) {      // declare new node , set top     stacknode *node;     node = malloc(sizeof(nodesize));      node->data = value;     node->nextptr = *topptr;      // reassign pointer top     *topptr = node;      printstack(*topptr); }  int pop(stacknodeptr *topptr) {      stacknode *node = *topptr;      if (isempty(node)) {         return 0;     }      // grab current top node , reset 1 beneath top pointer.     *topptr = node->nextptr;      printstack(*topptr);      return node->data; }  int isempty(stacknodeptr topptr) {     if (topptr == null || topptr->nextptr == null) { // bad_access         return 1;     }     return 0; }  void printstack(stacknodeptr topptr) {      stacknode *iter = topptr;      // if stack empty, print that.     if (!isempty(iter->nextptr)) {          // iterate on each element in stack , print data         (;isempty(iter); iter = iter->nextptr) {             printf("%d ", iter->data);         }         printf("null");     } else {         printf("null");     }      printf("\n"); }  void evaluatepostfixexpression(char *expr) {      // create stack , insert initial parenthesis     stacknode *head;     head = malloc(nodesize);      (int = 0; expr[i] != '\0'; i++) {         char token = expr[i];          // if current character digit         if (token >= '0' && token <= '9') {             push(&head, token - '0');          // if it's operator         } else if (token == '+' || token == '-' || token == '*' || token == '/' || token == '^' || token == '%') {              int x = pop(&head);             int y = pop(&head);              int result = calculate(y, x, token);             push(&head, result);         }     }      int output = pop(&head);      printf("the value of expression is: %d\n", output); }  int calculate(int op1, int op2, char operator) {     switch (operator) {         case '+':             return op1 + op2;         case '-':             return op1 - op2;         case '*':             return op1 * op2;         case '/':             return op1 / op2;         case '%':             return op1 % op2;     }      if (operator == '^') {         int result = 1;         (int = 0; < op2; i++) {             result *= op1;         }         return result;     }     return 0; } 

even hint in right direction appreciated.

request context:

int main() {     char postfix[255] = "54*81+4/+";      evaluatepostfixexpression(postfix);      return 0; }  void evaluatepostfixexpression(char *expr) {      // create stack , insert initial parenthesis     stacknode *head;     head = malloc(nodesize);      (int = 0; expr[i] != '\0'; i++) {         char token = expr[i];          // if current character digit         if (token >= '0' && token <= '9') {             push(&head, token - '0');          // if it's operator         } else if (token == '+' || token == '-' || token == '*' || token == '/' || token == '^' || token == '%') {              int x = pop(&head);             int y = pop(&head);              int result = calculate(y, x, token);             push(&head, result);         }     }      int output = pop(&head);      printf("the value of expression is: %d\n", output); }  int calculate(int op1, int op2, char operator) {     switch (operator) {         case '+':             return op1 + op2;         case '-':             return op1 - op2;         case '*':             return op1 * op2;         case '/':             return op1 / op2;         case '%':             return op1 % op2;     }      if (operator == '^') {         int result = 1;         (int = 0; < op2; i++) {             result *= op1;         }         return result;     }     return 0; } 

it looks expression

if (topptr == null || topptr->nextptr == null) 

is giving error because topptr doesn't point valid node, nor it's null. topptr->nextptr give error, due fact topptr not valid pointer.

according code, head->nextptr not initialized null, seems cause of problem.

by way: why initialize head variable empty node? far can see, use head = null , work.

last, not least: should free node variable in pop() function, in order avoid memory leak.

hope helps.


Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -