title: When will memory used in a function become free ??(C programming)
body: <p>Below is the code</p>
  
  <p><b>The Code:</b></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int * num(void);
  
  int main(void)
  {
      int * num2;
      num2 =num();
      printf("%d\n" , *num2);
  
      return 0;
  }
  
  
  int * num(void)
  {
      int num = 20;
  
      return &amp;num;
  }
  </code></pre>
  
  <p><b> The Question :</b></p>
  
  <ol>
  <li><p>As we know , the function <code>num</code> is local to its function <code>num()</code> , so in this code I try to return the address of the variable <code>num</code> in the function to the function that calls it , which is <code>main()</code>.</p></li>
  <li><p>After that I just use the dereferencing operator to extract the value of the specific <code>num</code> variable and print it out in <code>main()</code> function.</p></li>
  <li><p>There's one thing i'm confused . I remember i read a book about javascript that mention a variable lifetime is within the function , which mean after the function finish performing its instructions and pass the control back to the function that calls it , the value of each variable in the function will be clean out(garbage collector).But why in this code my <code>main()</code> function still can point to the value of that specific memory address??</p></li>
  </ol>