Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Segmentation violation in mex file ...
Replies: 1   Last Post: Feb 15, 2013 10:25 AM

 Steven Lord Posts: 18,038 Registered: 12/7/04
Re: Segmentation violation in mex file ...
Posted: Feb 15, 2013 10:25 AM

"Mahendra " <samarawickrama@gmail.com> wrote in message
news:kfk82i\$dig\$1@newscl01ah.mathworks.com...
> Hi,
>
> Following code run at first time, but second time it will crash because of
> Segmentation violation ...
>
> /* element-wise multiply of two double complex arrays of exactly the same
> size */
>
> #include "mex.h"
>
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray
> *prhs[])
> { int i;
> mwSignedIndex lda, nda;
> double *Apr, *Api, *Bpr, *Bpi, *Cpr, *Cpi;
> lda = (mwSignedIndex)mxGetM(prhs[0]); nda =
> (mwSignedIndex)mxGetN(prhs[0]);

You're assuming the first input is 2-D not N-D (with N > 2.) mxGetN will
work even for 3+ dimensional arrays, but the output created using nda and
lda will be of a different shape than the inputs.. Consider using
mxGetDimensions if there's a possibility the inputs could be 3+ dimensional
arrays.

> Apr = mxGetPr(prhs[0]);
> Api = mxGetPi(prhs[0]);
> Bpr = mxGetPr(prhs[1]);
> Bpi = mxGetPi(prhs[1]);

You're assuming the inputs are not sparse; if they were you'd need to work
with the ir and jc as well or convert it to full.

> plhs[0] = mxCreateDoubleMatrix(nda, lda, mxCOMPLEX);

Check that mxCreateDoubleMatrix succeeded before using its return value.

> Cpr = mxCalloc(nda, sizeof(double));
> Cpi = mxCalloc(nda, sizeof(double));
> Cpr = mxGetPr(plhs[0]);
> Cpi = mxGetPi(plhs[0]);

You allocate memory for Cpr and Cpi, then immediately throw away that
allocated memory and store the pr and pi from the output argument in those
variables. This is a memory leak.

> for( i=0; i<nda; i++ ) {
> *(Cpr+i) = (*Apr+i)*(*Bpr+i) - (*Api+i)*(*Bpi+i);
> *(Cpi+i) = (*Apr+i)*(*Bpi+i) + (*Api+i)*(*Bpr+i);

You're making a couple assumptions here. Some of the assumptions are:

1) A and B are both complex. If not, their pi is NULL and dereferencing NULL
isn't good.
2) A and B both have at least nda elements. If they don't, you'll go
marching off the end of one of them and that's not good either.
3) lda is not 0. If it is then the output argument in plhs[0] doesn't have
any elements, so assigning into one isn't good.

Your function also will "work" if A and B have the same number of elements
but different sizes (like 2-by-3 and 3-by-2) but it probably won't do what
you expect. Because of this I'd add in some decent prequalification of the
inputs. I'd also modify this to handle the case where one or both of Api or
Bpi are NULL.

You're also doing something strange by creating the output as an nda-by-lda
complex matrix but only filling up nda elements of it. What about the
remaining nda*(lda-1) elements?

> What might be the problem?

If I had to guess, I'd say the first time you called this you probably
called it with two complex inputs and the second time you didn't. But since
you didn't show how you called this, we can't be certain.

--
Steve Lord
slord@mathworks.com