From: emrah bostan on
hi,

i have a mex file including the following code.

*************
#include "mex.h"
#include "stdio.h"


void build_arr(double* x, double* supp,int N, int K, int C)
{

// Cost Array
double ***cost;
cost = new double** [N];
for( int i=0; i<N; i++ )
{
cost[i] = new double* [K+1];
for( int k=0; k<K+1; k++ )
{
cost[i][k] = new double [C+1];
for( int c=0; c<C+1; c++)
{
cost[i][k][c] = 1;
}
}
}

// Solution Array
int ****sol;
sol = new int*** [N];
for( int i=0; i<N; i++)
{
sol[i] = new int** [K+1];
for( int k=0; k<K+1; k++ )
{
sol[i][k] = new int* [C+1];
for( int c=0; c<C+1; c++ )
{
sol[i][k][c] = new int [N];
for( int n=0; n<N; n++ )
{
sol[i][k][c][n] = 0;
}
}
}
}

// Destruct Solution Array
for( int i=0; i<N; i++ )
{
for( int k=0; k<K+1; k++ )
{
for( int c=0; c<C+1; c++ )
{
delete [] sol[i][k][c];
}
delete [] sol[i][k];
}
delete [] sol[i];
}
delete [] sol;
sol = 0;

// Destruct Cost Array
for (int i=0; i<N; ++i)
{
for (int k=0; k<K+1; ++k)
{
delete [] cost[i][k];
}
delete [] cost[i];
}
delete [] cost;
cost = 0;
}
/* the gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *x, *supp;
int N;
int K, C;
int mrows,ncols;

/* check for proper number of arguments */
/* NOTE: You do not need an else statement when using mexErrMsgTxt
within an if statement, because it will never get to the else
statement if mexErrMsgTxt is executed. (mexErrMsgTxt breaks you out of
the MEX-file) */
if(nrhs!=3)
mexErrMsgTxt("Three inputs required: x, K, C");
if(nlhs!=1)
mexErrMsgTxt("One output required.");

/* check to make sure the last two input arguments are scalar */
if( !mxIsDouble(prhs[1]) || mxGetN(prhs[1])*mxGetM(prhs[1])!=1 ) {
mexErrMsgTxt("Input K must be a scalar.");
}

if( !mxIsDouble(prhs[2]) || mxGetN(prhs[2])*mxGetM(prhs[2])!=1 ) {
mexErrMsgTxt("Input C must be a scalar.");
}

/* get the scalar input K */
K = mxGetScalar(prhs[1]);

/* get the scalar input C */
C = mxGetScalar(prhs[2]);

/* create a pointer to the input matrix y */
x = mxGetPr(prhs[0]);

/* get the dimensions of the matrix input xx */
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if (mrows>ncols)
N = mrows;
else
N = ncols;


/* set the output pointer to the output matrix */
plhs[0] = mxCreateDoubleMatrix(mrows,ncols,mxREAL);

/* create a C pointer to a copy of the output matrix */
supp = mxGetPr(plhs[0]);

/* call the C subroutine */
build_arr(x,supp,N,K,C);

}

*************

i am just trying to see if i can succesfully construct and destruct my arrays. For x=rand(1,1000), K=100 and C=3, the code gives the following error on my computer;

??? Unexpected Standard exception from MEX file.
What() is:St9bad_alloc

But it just works fine for the same values on another computer. Any help is welcome.
cheers