Error while removing stack item

Asked

Viewed 183 times

2

Error of the function pop??

void pop(Pilha *pilha){
  if(pilha->size == 0){
    puts("pilha vazia");
  } else {
    printf("item removido: %d\n\n", pilha->itens[--pilha->size]);
    free(pilha->itens[--pilha->size]);
  }
}

Link to Stack.

2 answers

5

You decrease the value of pilha->size twice. It is right to do it only once.

-3

The stack size is decreasing more than once per loop. Remember that -- really changes the value of the variable.

void pop(Pilha *pilha){
  if(pilha->size == 0){
    puts("pilha vazia");
  } else {
    printf("item removido: %d\n\n", pilha->itens[--pilha->size]);
    free(pilha->itens[pilha->size]);
  }
}
  • Probably this code will give execution problem in the second withdrawal...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.