Change it to add a second call to printf , and you will see a different value the first time. Compile it with optimizations enabled, and you will see a different set of values. Do something with a value, and you go to undefined territory, which means that the compiler can call daemons through your nasal passages.
On my system, I see 50 , and then 0 ; with optimizations I see 0 and then 32767 .
If you create a local static variable, you can return its address, since it will become like a global one (but remember that there is only one instance).
When the function returns, the local memory that it used on the stack is now considered an "unused" program, since the stack no longer reaches this maximum. However, as a rule, values ββstill exist, since there is no need to urgently clean them. The memory also still belongs to the program, since there is no point in returning several bytes at a time to the memory operating system. So, for your specific example, in the conditions in which you compiled it, the memory that it still points to contains a value of 50 . Officially, however, the value of *p is undefined, and attempts to use it lead to undefined behavior.
One existential C language crisis is how, on the one hand, it says nothing about the stack and the various bits of hexadecimal sediment that make up the current process; on the other hand, you need to understand them in order to protect yourself from crashes, buffer overflows and undefined behavior. Just remember that you are lucky that the GCC gives a warning for this.
source share