Is it OK to reference an out-of-scope local variable within the same function?
c++, pointers, scope
Solution
No, that's not guaranteed to work. `a` is dangling once the inner scope is exited, so any dereference of it results in Undefined Behaviour and nothing whatsoever is guaranteed.
Problem
In this code, I reference the local variable `b` even though it is out of scope. But I do it from within the same function so it's probably still on the stack, right? I ran the program and it worked but I'd like to know if it's guaranteed to work on all implementations. ``` #include <iostream> void main() { int* a; { int b = 5; a = &b; } std::cout << *a; } ```