title: Deleting a doublepointer (matrix)
body: <p>I am solving a quantum-mech problem which requires me to find some eigenvalues by manipulating some matrices. The specifics of this problem is not relevant, I just need help with the c++ problem, I am new to this language and after a couple of hours I figured any more attempts at solving it myself would be futile and so I turn to you for help.</p>
  
  <p>I have this problem where glibc detects an error at the end of my program and I cannot deallocate properly, it is far too big to copypaste here so I will just replicate the part that actually gives the error.</p>
  
  <pre><code>void hamiltonian(int, double **&amp;);
  
  int i,j;
  
  int main()
  {
  int N = 1000; double **A;
  
  hamiltonian(N, A);
  
  //Physics here
  .
  .
  .
  .
  .
  //Delete
  for(i=0; i&lt;N; i++){delete []A[i];}
  delete []A;
  
  return 0;
  }
  
  void hamiltonian(int N, double **&amp;A)
  {
  A = new double *[N];
  for(i=0; i&lt;N; i++)
  {
  A[i] = new double[N];
  for(j=0; j&lt;N; j++)
  {
  if(i==j)A[i][j] = 2;
  if(i==j+1 || i==j-1){A[i][j] = 1;}
  }
  }
  }
  </code></pre>
  
  <p>According to my professor I have to deallocate in the same function as I allocate but I didn't even think about deallocation after being nearly done with my project and so I have to rewrite a lot of code, the problem is that I cannot deallocate A inside the hamiltonian function as I need it in other functions (inside //Physics).</p>
  
  <p>Surely there must be a way around this? Might sound a bit ignorant of me but this sounds like a less efficient design if I have to deallocate in the same function as I allocate.</p>