Error removing stack item

2

What is the 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 from Stack .

    
asked by anonymous 27.02.2014 / 03:00

2 answers

5

You decrement the value of pilha->size twice. The right thing is to do it only once.

    
27.02.2014 / 03:04
-3

The size of the stack is decremented more than once per loop. Remember that -- actually 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]);
  }
}
    
27.02.2014 / 03:23