Let's say we have the following code. st is a global stack and is initialized properly. We call foo()
struct stack *st;
int foo() {
int x = 1;
stack_push(st, &x);
}
Now, the stack contains a pointer to x. After foo goes out of scope, what happens to the address &x? Is it deallocated? Can we trust that when we pop() the stack later on and dereferenced it that the value will be 1?
edit: Here's what I'm currently thinking. Since x is allocated on the stack, once it goes out of scope, the memory occupied in address &x can be used by anyone therefore the dereferenced value returned by pop() later on can possibly be not 1.