[Solved] Use memory on stack


why not the program crash, or print some random garbage?

The program will not crash as you are not accessing any illegal memory. The stack memory is a part of your program and as long as you are accessing the memory in a valid range the program will not crash. Yes, you can modify the stack memory whether the function is in that stack frame or not.

Now accessing the memory which is not in the current stack frame will lead to an Undefined Behavior. This is compiler dependent. Most of the compiler will print the garbage value. I don’t know which compiler you are using!!

I would say try to understand the basic concept of stack memory in C/C++ programs. An I would suggest also look into heap memory.

solved Use memory on stack