title: My code crashes when calling my solve_chol() function using the lapack library
body: <pre><code>#include "mex.h"
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  
  extern int dpotrs(char *, int *, int *, double *, int *, double *, int *, int *);
  
  void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  {
    double *C;
    int n, m, q;
  
    if (nrhs != 2 || nlhs &gt; 1)                              /* check the         input */
      mexErrMsgTxt("Usage: X = solve_chol(R, B)");
  
    n = mxGetN(prhs[0]);
  
    if (n != mxGetM(prhs[0]))
      mexErrMsgTxt("Error: First argument matrix must be square");
  
    if (n != mxGetM(prhs[1]))
      mexErrMsgTxt("Error: First and second argument matrices must have same     number of rows");
  
    m = mxGetN(prhs[1]);
    plhs[0] = mxCreateDoubleMatrix(n, m, mxREAL); /* allocate space for output */
    C = mxGetPr(plhs[0]);
  
    if (n==0) return;              /* if argument was empty matrix, do no more */
      memcpy(C,mxGetPr(prhs[1]),n*m*sizeof(double));   /* copy argument matrix */
      dpotrs("U", &amp;n, &amp;m, mxGetPr(prhs[0]), &amp;n, C, &amp;n, &amp;q);   /* solve system */
  
    if (q &gt; 0)
      mexErrMsgTxt("Error: illegal input to solve_chol");
  }
  </code></pre>
  
  <p>Hello guys,</p>
  
  <p>I am having a problem with this C-code. It is supposed to solve A matrix system <code>(Ax = B)</code> with <code>A</code> being symmetric, positive definite and already in upper triangular form?</p>
  
  <p>I created this snippet to boost matlab performance, but unfortunately matlab crashes and some debugging showed that the problem is the call of <code>dpotrs()</code> towards the end of the script.
  Can you see what the problem might be? </p>
  
  <p>Information on <code>dpotrs()</code> can be found here: <a href="http://physics.oregonstate.edu/~rubin/nacphy/lapack/routines/dpotrs.html" rel="nofollow">http://physics.oregonstate.edu/~rubin/nacphy/lapack/routines/dpotrs.html</a></p>
  
  <p>Would be great if you could help</p>