Change memory allocation for mex function (..again..)

fixes-turtlebasket
Mattia 2019-07-09 17:29:18 +01:00
parent 9b878ec71c
commit 214af94bd0
1 changed files with 10 additions and 25 deletions

View File

@ -831,13 +831,12 @@ void mexFunction( int nlhs, mxArray *plhs[],
double *inCoordsB; double *inCoordsB;
size_t nCoordsA; size_t nCoordsA;
size_t nCoordsB; size_t nCoordsB;
int i,j; int i;
double *distance; double *distance;
int c = 3; int c = 3;
int count = 0; int count = 0;
int len1 = 0, len2 = 0; double**arr1;
double *ptr1, **arr1; double**arr2;
double *ptr2, **arr2;
/**************** PARSE INPUTS AND OUTPUTS **********************/ /**************** PARSE INPUTS AND OUTPUTS **********************/
/*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/
@ -887,29 +886,15 @@ void mexFunction( int nlhs, mxArray *plhs[],
/* get a pointer to the real data in the output matrix */ /* get a pointer to the real data in the output matrix */
distance = mxGetPr(plhs[0]); distance = mxGetPr(plhs[0]);
/* Copy data from Matlab's vectors into two new arrays */ /* Copy data from Matlab's vectors into two new arrays */
len1 = sizeof(double *) * (int) nCoordsA + sizeof(double) * c * (int) nCoordsA; arr1 = (double **)mxMalloc( sizeof(double *) * (int)nCoordsA );
arr1 = (double **)mxMalloc(len1); arr2 = (double **)mxMalloc( sizeof(double *) * (int)nCoordsB );
len2 = sizeof(double *) * (int) nCoordsB + sizeof(double) * c * (int) nCoordsB;
arr2 = (double **)mxMalloc(len2);
ptr1 = (double *)(arr1 + nCoordsA);
ptr2 = (double *)(arr2 + nCoordsB);
for (i = 0; i < nCoordsA; i++) for (i = 0; i < nCoordsA; i++)
arr1[i] = (ptr1 + c * i); arr1[i] = &inCoordsA[i * 3];
for (i = 0; i < nCoordsB; i++)
arr2[i] = (ptr2 + c * i);
for (j = 0; j < nCoordsA; j++) for (i = 0; i < nCoordsB; i++)
for (i = 0; i < 3; i++) arr2[i] = &inCoordsB[i * 3];
arr1[j][i] = inCoordsA[i + j * 3];
for (j = 0; j < nCoordsB; j++)
for (i = 0; i < 3; i++)
arr2[j][i] = inCoordsB[i + j * 3];
/*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/
/* POPULATE BODIES' STRUCTURES */ /* POPULATE BODIES' STRUCTURES */