Fields of a struct after freeing it - C
Hey all.
Suppose we have the following struct definition of Stack data structure implementation:
Code:
typedef struct StrStackLink {
char* str;
struct StrStackLink *next;
} StrStackLink;
struct StrStack {
StrStackLink* top;
};
Where its Pop method implementation is shown below:
Code:
char* StrStackPop(StrStack* stack)
{
char *s;
StrStackLink *p;
assert( stack != NULL );
if (stack->top == NULL) {
return NULL;
}
s = stack->top->str;
p = stack->top; // This lives in the HEAP
stack->top = p->next;
free(p);
return s;
}
My question is about the line:
Code:
s = stack->top->str;
Why is it correct?
After we free p struct (which is the former top), its str field is not "disappeared"?
Thanks in advance!