title: What happens when a variable goes out of scope?
body: <p>In most managed languages (that is, the ones with a GC), local variables that go out of scope are inaccessible and have a higher GC-priority (hence, they'll be freed first).</p>
  
  <p>Now, C is not a managed language, what happens to variables that go out of scope here?</p>
  
  <p>I created a small test-case in C:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(void){
      int *ptr;
  
      {
          // New scope
          int tmp = 17;
          ptr = &amp;tmp; // Just to see if the memory is cleared
      }
  
      //printf("tmp = %d", tmp); // Compile-time error (as expected)
      printf("ptr = %d\n", *ptr);
  
      return 0;
  }
  </code></pre>
  
  <p>I'm using GCC 4.7.3 to compile and the program above prints <code>17</code>, why? And when/under what circumstances will the local variables be freed?</p>