Can a local variable's memory be accessed outside its scope?
问题 I have the following code. #include <iostream> int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the code is just running with no runtime exceptions! The output was 58 How can it be? Isn't the memory of a local variable inaccessible outside its function? 回答1: How can it be? Isn't the memory of a local variable inaccessible outside its function? You rent a hotel room. You put a book in the top drawer of the bedside table and go