mex file compiles fine but crashes when run (c code run externally runs no problem)

1 Ansicht (letzte 30 Tage)
As the title states I'm able to run a piece of code outside mex without any problem but matlab crashes when run as mex.
the twiddle code is from here
CODE:
#include "math.h"
#include "mex.h"
#define PI M_PI
/*twiddle.c - generate all combinations of M elements drawn without replacement
from a set of N elements.
*/
mwSize twiddle(mwSize *x, mwSize *y, mwSize *z, mwSize *p)
{
mwSize i, j, k;
j = 1;
while(p[j] <= 0)
j++;
if(p[j-1] == 0)
{
for(i = j-1; i != 1; i--)
p[i] = -1;
p[j] = 0;
*x = *z = 0;
p[1] = 1;
*y = j-1;
}
else
{
if(j > 1)
p[j-1] = 0;
do
j++;
while(p[j] > 0);
k = j-1;
i = j;
while(p[i] == 0)
p[i++] = -1;
if(p[i] == -1)
{
p[i] = p[k];
*z = p[k]-1;
*x = i-1;
*y = k-1;
p[k] = -1;
}
else
{
if(i == p[0])
return(1);
else
{
p[j] = p[i];
*z = p[i]-1;
p[i] = 0;
*x = j-1;
*y = i-1;
}
}
}
return(0);
}
void inittwiddle(mwSize m, mwSize n, mwSize *p)
{
mwSize i;
p[0] = n+1;
for(i = 1; i < n-m+1; i++)
p[i] = 0;
while(i < n+1)
{
p[i] = i+m-n;
i++;
}
p[n+1] = -2;
if(m == 0)
p[1] = 1;
}
/* */
void createConstMbyNaSM(mwSize Nt, mwSize Nr, mwSize M_full, double alpha, mwSize numsym,
double *all_symr, double *all_symi)
{
mwSize constSize = 0, iNa;
mwSize M, temp, i, iM, iMgray;
mwSize x, y, z, *p, *b;
double *temp_symr, *temp_symi;
temp_symr = all_symr;
temp_symi = all_symi;
p = mxMalloc(Nt+2 * sizeof(*p));
b = mxMalloc(Nt * sizeof(*b));
for (iNa = 1; iNa < (Nt-1); iNa++)
{
temp = iNa;
M = M_full;
/*while(temp >>= 1)
M = M/2;*/
inittwiddle(iNa, Nt, p);
/*for(i = 0; i != Nt-iNa; i++)
b[i] = 0;
while(i != Nt)
b[i++] = 1;
for (i = 0; i < Nt; i++)
{
*(temp_symr+constSize) = *(apm_symr+iM) * b[i];
*(temp_symi+cobstSize) = *(apm_symi+iM) * b[i];
constSize++;
}
while(!twiddle(&x, &y, &z, p))
{
b[x] = 1;
b[y] = 0;
for (iM = 0; iM < M; iM++)
{
for (i = 0; i < Nt; i++)
{
/*iMgray = iM ^ (iM >> 1);
/**(temp_symr+constSize) = cos(iMgray * 2*PI/M) * b[i];
*(temp_symi+constSize) = sin(iMgray * 2*PI/M) * b[i];
constSize++;
}
constSize++;
}
}*/
constSize++;
/* Free memory Allocation */
/*all_symr[iNa] = constSize;*/
}
mxFree(b);
mxFree(p);
}
/* The gateway routine. */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *all_symr, *all_symi, alpha;
mwSize Nt, Nr, M, num_sym;
/* check for the proper number of arguments */
if(nrhs != 5)
mexErrMsgIdAndTxt( "MATLAB:recvSM:invalidNumInputs",
"Invalid number of inputs.");
if(nlhs > 1)
mexErrMsgIdAndTxt( "MATLAB:recvSM:maxlhs",
"Too many output arguments.");
Nt = mxGetScalar(prhs[0]);
Nr = mxGetScalar(prhs[1]);
M = mxGetScalar(prhs[2]);
alpha = mxGetScalar(prhs[3]);
num_sym = mxGetScalar(prhs[4]);
plhs[0] = mxCreateDoubleMatrix(Nt, (mwSize) num_sym, mxCOMPLEX);
all_symr = mxGetPr(plhs[0]);
all_symi = mxGetPi(plhs[0]);
createConstMbyNaSM(Nt, Nr, M, alpha, num_sym, all_symr, all_symi);
}
  1 Kommentar
Pranav Koundinya
Pranav Koundinya am 2 Sep. 2015
I have commented out everything after call to inittwiddle in the for loop to check if that function is the cause (and it seems to be). But this compiles and runs fine outside matlab
On a side note I am on Ubuntu so debugging is hard with gdb

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by