|
Hi again
I am trying to implement a very simple subroutine embeding a CMEX code in Matlab.
Does anybody know if it is possible that I get a "Segmentation violation" error even if I don't go beyond the iput arrays' limits?
I have checked the code with small inputs, and works fine. But when I send a rather huge array, it crashes.
This is the code:
#include "mex.h"
/*
* Calculate_I_Values.c
* Used to speed up the I mapping calculations
*
* This is a MEX-file for MATLAB.
*/
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* variable declarations here */
double *e, *x, *y, *I;
int eRows, eCols, xRows, xCols, yRows, yCols, index, ySize;
int m, i, j;
double num, den;
e = mxGetPr(prhs[0]);
eRows = mxGetN(prhs[0]);
eCols = mxGetM(prhs[0]);
x = mxGetPr(prhs[1]);
xRows = mxGetN(prhs[1]);
xCols = mxGetM(prhs[1]);
y = mxGetPr(prhs[2]);
yRows = mxGetN(prhs[2]);
yCols = mxGetM(prhs[2]);
ySize = mxGetNumberOfElements(prhs[2]);
/* Allocate output memory */
plhs[0] = mxCreateDoubleMatrix(1, 256, mxREAL);
I = mxGetPr(plhs[0]);
/* code here */
printf("The xNumRow is: %d\n", (int)xRows);
printf("The xNumCol is: %d\n\n", (int)xCols);
printf("The yNumRow is: %d\n", (int)yRows);
printf("The yNumCol is: %d\n\n", (int)yCols);
printf("The ySize is: %d\n\n", (int)ySize);
printf("The eNumRow is: %d\n", (int)eRows);
printf("The eNumCol is: %d\n\n", (int)eCols);
for (m = 0; m <= 255; m++)
{
printf("m = %d -> ", (int)m);
num = 0;
den = 0;
for (i = 0; i < yRows; i++)
for (j = 0; j < yCols; j++)
{
index = i * yCols + j;
if (index >= ySize)
printf("Wrong\n");
else if (m == y[index])
{
num += e[j] * x[i];
den++;
}
}
if (den == 0) I[m] = num;
else I[m] = num / den;
}
}
It is actually pretty simple, but...
I call the function by:
Calculate_I_Values(exposures.', x.', y.');
Obviously after compiling.
The first printf lines give me:
The xNumRow is: 361862
The xNumCol is: 1
The yNumRow is: 361862
The yNumCol is: 16
The ySize is: 5789792
The eNumRow is: 1
The eNumCol is: 16
So everyting is as it should be. But then I get the segmentation violation error.
I have commented different parts of the program, and concluded that the error is when I do y[index]. So I guess I am trying to access a memory address which is shouldn't...
But I have already controlled that the index does not exceed the size of the input array! (ySize)
Any clue would be appreciated :)
Thanks in advance
Jose
|