title: Global pointer to a local variable in function
body: <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int *ptr;
  
  void func1() {
      int a = 10;
      ptr = &amp;a;
  }
  
  int main()
  {
      func1()
      cout &lt;&lt; *ptr &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>./a.out gives 10 correctly.</p>
  
  <p>In the above code, 'a' is the local variable whose scope will be within func1(). As per my understanding when the method is activated, a block of memory cells, called activation record, is allocated, which contains all local variables and formal parameters of the current method call. The activation record is used during the method execution, and is then removed at the end of the execution. When the activation record is removed, the memory locations for the local variables and for the formal parameters are destroyed and the values they contain are lost. But here, at main function when printing the value stored in the memory address of 'a' gives 10 correctly. Pl clarify.</p>