title: Is there any memory issue with the way I handled this?
body: <p>I am writing a wrapper to Eigen QR for my personal use and I am wondering if there is any memory leak or undocumented behavior in my implementation especially in the function <code>void get_QR(double* A, int m, int n, double*&amp; Q, double*&amp; R)</code>. The answer is as expected. This is related to my previous question <a href="https://stackoverflow.com/questions/22103526/weird-behavior-when-using-eigen">here</a>.</p>
  
  <pre><code>using std::cout;
  using std::endl;
  using namespace Eigen;
  
  /*!
   Obtains the QR decomposition as A=QR, where all the matrices are in Eigen MatrixXd format.
   */
  void get_QR(MatrixXd A, MatrixXd&amp; Q, MatrixXd&amp; R) {
  
          int m           =       A.rows();
          int n           =       A.cols();
          int minmn       =       min(m,n);
  
          //      A_E     =       Q_E*R_E.
          HouseholderQR&lt;MatrixXd&gt; qr(A);
          Q  =   qr.householderQ()*(MatrixXd::Identity(m, minmn));
          R  =   qr.matrixQR().block(0, 0, minmn, n).triangularView&lt;Upper&gt;();
  }
  
  /*!
   Obtains the QR decomposition as A=QR, where all the matrices are in double format.
   */
  void get_QR(double* A, int m, int n, double*&amp; Q, double*&amp; R) {
          MatrixXd Q_E, R_E;
          int minmn       =       min(m,n);
  
          //      Maps the double to MatrixXd.
          Map&lt;MatrixXd&gt; A_E(A, m, n);
  
          get_QR(A_E, Q_E, R_E);
  
          Q       =       (double*)realloc(Q_E.data(), m*minmn*sizeof(double));
          R       =       (double*)realloc(R_E.data(), minmn*n*sizeof(double));
  }
  
  
  int main(int argc, char* argv[]) {
          srand(time(NULL));
  
          int m   =       atoi(argv[1]);
          int n   =       atoi(argv[2]);
  
  //      Check the double version.
          int minmn       =       min(m,n);
          double* A       =       (double*)malloc(m*n*sizeof(double));
          double* Q       =       (double*)malloc(m*minmn*sizeof(double));
          double* R       =       (double*)malloc(minmn*n*sizeof(double));
  
          double RANDMAX  =       double(RAND_MAX);
          //      Initialize A as a random matrix.
          for (int index=0; index&lt;m*n; ++index) {
                  A[index]        =       rand()/RANDMAX;
          }
  
          get_QR(A, m, n, Q, R);
          std::cout &lt;&lt; Q[0] &lt;&lt; std::endl;
  
  //      Check the MatrixXd version.
          Map&lt;MatrixXd&gt; A_E(A, m, n);
          MatrixXd Q_E, R_E;
          get_QR(A_E, Q_E, R_E);
  
          cout &lt;&lt; Q[0] &lt;&lt; endl;
          cout &lt;&lt; Q_E(0,0) &lt;&lt; endl;
  
          free(A);
          free(Q);
          free(R);
  }
  </code></pre>
  
  <p>For instance, I get the output as</p>
  
  <pre><code>-0.360995
  -0.360995
  -0.360995
  </code></pre>