title: local memory address is valid after function return?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope">Can a local variable&#39;s memory be accessed outside its scope?</a>  </p>
  </blockquote>
  
  
  
  <p>I was refreshing the knowledge about how memory internally works, and I faced the confusion. Here is sample code</p>
  
  <pre><code>int * func(){
     int retval = 3;
     return &amp;retval;
  }
  
  int main(void){
     int *ptr = func();
     printf("address return from function %p and value %d\n", ptr, *ptr);
  }
  </code></pre>
  
  <p>My understanding regards how stack memory works on a routine, is when a function was called, it is pushed on the stack. And lifetime of local variables within this routine would no longer valid once the function returns. So returning address of local variable seems like not valid, but when I test this code, it actually returns its address and still valid after the function returns. </p>
  
  <p>am I misunderstanding the concept ? Appreciated any comments, Thanks.</p>