title: Why struct variable goes out of scope
body: <p>In the below code, Why i couldn't able to access test_var from main? My assumptions are that new allocates memory in heap, so the lifetime is till the end of the main, or till you explicitly delete it. But when i try to access test_var, i get exception.</p>
  
  <pre><code>typedef struct test{
      int a;
      string str;
  }test;
  
  void fun1(test* test_var)
  {
      test_var = new test[2];
      test_var[0].a=1;
      test_var[0].str='a';
      test_var[1].a = 2;
      test_var[1].str = 'b';
      return;
  }
  
  int main()
  {
      test *test_var = NULL;
      fun1(test_var);
      cout&lt;&lt;test_var[0].str;
      delete test_var;            
      return 1;
  }
  </code></pre>