MATLAB crashes when running mex file with recursive function

I created a mex file and compiled it successfully but MATLAB crashes when I run the file. Below is the mex file I created. It uses a recursive function.
# include "mex.h"
double B(mwSize l,double *t_arr,mwSize m,mwSize p,double *t_array){
double b,w1,w2;
if (t_arr[l]>=t_array[m] && t_arr[l]<=t_array[m+1] && p==1)
{ b=1;
}
else if (p>1){
if ((t_array[m+p-1]-t_array[m])==0)
{
w1=0;
}
else{
w1=(t_arr[l]-t_array[m])/(t_array[m+p-1]-t_array[m]);
}
if ((t_array[m+p-1]-t_array[m+1])==0)
{
w2=0;
}
else{
w2=(t_arr[l]-t_array[m+1])/(t_array[m+p-1]-t_array[m+1]);
}
b=(w1*B(l,t_arr,m,p-1,t_array))+((1-w2)*B(l,t_arr,m+1,p-1,t_array));
}
else
{ b=0;
}
return b;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Check number of inputs:
if (nrhs != 5) {
mexErrMsgIdAndTxt("JSimon:Func:BadNInput",
"5 inputs required.");
}
if( !mxIsDouble(prhs[0]) ||
mxIsComplex(prhs[0]) ||
mxGetNumberOfElements(prhs[0]) != 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar",
"Input multiplier must be a scalar.");
}
/* check that number of rows in second input argument is 1 */
if(mxGetM(prhs[1]) != 1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector",
"Input must be a row vector.");
}
if( !mxIsDouble(prhs[2]) ||
mxIsComplex(prhs[2]) ||
mxGetNumberOfElements(prhs[2]) != 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar",
"Input multiplier must be a scalar.");
}
if( !mxIsDouble(prhs[3]) ||
mxIsComplex(prhs[3]) ||
mxGetNumberOfElements(prhs[3]) != 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notScalar",
"Input multiplier must be a scalar.");
}
/* check that number of rows in second input argument is 1 */
if(mxGetM(prhs[4]) != 1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector",
"Input must be a row vector.");
}
double *t_arr, *t_array; // inputs
double b; // output
mwSize l, m, p;
// Read inputs:
l = (mwSize) mxGetScalar(prhs[0]);
m = (mwSize) mxGetScalar(prhs[3]);
p = (mwSize) mxGetScalar(prhs[4]);
t_arr = mxGetPr(prhs[2]);
t_array = mxGetPr(prhs[5]);
// Create output:
plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL);
b = (double) mxGetScalar(plhs[0]);
// The computations:
b=B(l,t_arr,m,p,t_array);
return;
}

댓글 수: 4

This is not the cause of the crash, but a mistake:
b = (double) mxGetScalar(plhs[0]);
You create a variable and reads its contents into the variable b. Later on you overwrite b, but this will not be forwarded to the caller. You need:
double *bp;
plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL);
bp = mxGetPr(plhs[0]);
*bp = B(l,t_arr,m,p,t_array);
Thanks Jan! But MATLAB still crashes when I run the file. Is there any specific way to run the file?
How are you calling this from your m-code? What are the argument types and sizes?
I saved the file as B.c. After compiling, I get the file B.mexw64
a=0;
b=10;
p=3;
n=17;
f=n+p;
t_arr=linspace(a,b,f);
t_array=linspace(a,b,1000);
b1=0;
for i = 1:(length(t_array))
bl=bl+(B(i,t_array,1,p,t_arr)*B(i,t_array,1,p,t_arr)); // calling here
end

댓글을 달려면 로그인하십시오.

 채택된 답변

James Tursa
James Tursa 2017년 11월 2일
편집: James Tursa 2017년 11월 2일
You've got your C mex indexing wrong. Remember, C is 0-based indexing, not 1-based indexing. So in these lines:
l = (mwSize) mxGetScalar(prhs[0]);
m = (mwSize) mxGetScalar(prhs[3]);
p = (mwSize) mxGetScalar(prhs[4]);
t_arr = mxGetPr(prhs[2]);
t_array = mxGetPr(prhs[5]);
The available indexes for prhs are 0,1,2,3,4 since you are calling the B mex routine with five inputs. That last line above uses prhs[5] which means you are reading beyond the valid indexing for prhs, so you get a memory access error and a seg fault. What you probably need to use is this:
l = (mwSize) mxGetScalar(prhs[0]);
m = (mwSize) mxGetScalar(prhs[2]);
p = (mwSize) mxGetScalar(prhs[3]);
t_arr = mxGetPr(prhs[1]);
t_array = mxGetPr(prhs[4]);

댓글 수: 3

Thanks James! But why doesn't it show an error?
Why doesn't what show an error? If you mean why doesn't the compiler complain, it is because the compiler has no clue how many variable pointers are behind the prhs pointer, so it cannot know that prhs[5] is out of bounds.
Yes I meant the compiler. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

질문:

2017년 11월 2일

댓글:

2017년 11월 2일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by