typedef struct StackElement{
struct StackElement* below;
float value;
} sStackElement;
typedef struct StackElement* stack_top;
float stack_pop(stack_top *stacktop)
{
sStackElement* temp = (sStackElement *) stacktop;
if(stacktop==NULL){
return NAN;
}
float temp2 = temp->value;
stacktop = &(temp->below);
free(temp);
return temp2;
}
I am trying to implement a stack pop function, but somehow I get a double free error.
In the function, I am creating a temporary pointer which points to the current stack:
- then I check if the pointer is Null.
- then I point top to the 2nd Element in the Stack.
- then I free temp (which points to the element on the top of the stack)
- then I get the double free error
What am I doing wrong?
temp->belowa pointersStackElement*? If so, you have an extra reference operator herestacktop = &(temp->below);- Eugene Sh.sStackElementandstack_top- Eugene Sh.&is a problem, see my answer - Eugene Sh.