Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Modify it to add a second call to <code>printf</code> and you'll see a different value from the first time. Compile it with optimizations turned on and you'll see another set of values. Do <em>anything</em> with the value and you're stepping into undefined territory, which means that the compiler is free to summon demons through your nasal passages.</p> <p>On my system, I see <code>50</code> and then <code>0</code>; with optimizations I see <code>0</code> and then <code>32767</code>.</p> <p>If you make the local variable <code>static</code>, then you can return its address since it becomes just like a global (but remember that there is only one instance of it).</p> <p>When a function returns, the local storage it was using on the stack is now considered "unused" by the program, since the stack doesn't go that high anymore. Typically, though, the values are still there, since there's no urgent need to clear them. The memory is also still owned by the program, since there's no sense in returning memory to the operating system a few bytes at a time. So for your specific example, under the circumstances in which you compiled it, the memory pointed to still contains the value <code>50</code>. Officially, though, the value of <code>*p</code> is <em>indeterminate</em>, and attempts to use it result in <em>undefined</em> behavior.</p> <p>One existential crisis of the C language is how on the one hand, it says nothing about the stack and the various bits of hexadecimal sludge that make up a running process; on the other hand, it's necessary to understand those in order to protect yourself from crashes, buffer overflows, and undefined behavior. Just remember that you're lucky that GCC gives a warning for this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload