Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news2.google.com!news.glorb.com!news2.glorb.com!tr22g12.aset.psu.edu!news.mathforum.org!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Segmentation violation due to MEX code
Date: Mon, 06 Jul 2009 08:43:24 EDT
Organization: The Math Forum
Lines: 115
Message-ID: <1861877.79321.1246884234900.JavaMail.jakarta@nitrogen.mathforum.org>
NNTP-Posting-Host: nitrogen.mathforum.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: support1.mathforum.org 1246884234 3703 144.118.30.135 (6 Jul 2009 12:43:54 GMT)
X-Complaints-To: news@news.mathforum.org
NNTP-Posting-Date: Mon, 6 Jul 2009 12:43:54 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:553055


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