开发者

Mex file gives segmentation fault only on second use

开发者 https://www.devze.com 2023-02-27 09:22 出处:网络
The following code compiles successfully and returns the correct results when called the first time. Making the same call a second time, I get a segmentation fault error.

The following code compiles successfully and returns the correct results when called the first time. Making the same call a second time, I get a segmentation fault error.

//% function TF = InWindow(Date,WindowStartDates,WindowEndDates,EndHint)
//% INWINDOW returns true for window that contains Date. All inputs must be 
//% uint32 and WindowEndDates must be sorted.

//% EndHint is an optional input that specifies the row number to start
//% searching from.

#include "mex.h"
#include "matrix.h"
#include "math.h"

void CalculationRoutine(mxLogical *ismember, uint32_T *Date, uint32_T *WindowStartDates, uint32_T *WindowEndDates, unsigned int *StartIndex, int NumObs) {

mwIndex Counter;

// Find the first window that ends on or after the date. 
for (Counter = (mwIndex) *StartIndex; Counter < NumObs; Counter++) {  
    if (*Date <= *(WindowEndDates+Counter)) {
        break;
    }
}
*StartIndex = (unsigned int) Counter;

// Now flag every observation within the window. Remember that WindowStartDates
// is not necessarily sorted (but WindowEndDates is).
for (Counter = (mwIndex) *StartIndex; Counter < NumObs; Counter++) { 
    if (*Date >= *(WindowStartDates+Counter)) {
        *(ismember+Counter) = true;
    }
}

}

void mexFunction( int nlhs, mxArray 开发者_StackOverflow中文版*plhs[], int nrhs, const mxArray *prhs[] ) {

    mxArray *ismember;
    unsigned int *StartIndex;

    //Input Checking.

    if (nrhs == 3) {
        // Default Hint to first entry.        
        mexPrintf("SI Starts OK.\n");
        *StartIndex = 1;
        mexPrintf("SI Ends OK.\n");
    } else if (nrhs == 4) {
        if (!mxIsUint32(prhs[3])) {
            mexErrMsgTxt("EndHint must be uint32.");
        } 
        StartIndex = mxGetData(prhs[3]);
    } else {
        mexErrMsgTxt("Must provide three or four input arguments.");
    }
    // Convert the hint to base-zero indexing.
    *StartIndex = *StartIndex - 1;

    // Check the inputs for the window range.
    if (!mxIsUint32(prhs[0])) {
        mexErrMsgTxt("DatesList must be uint32.");
    }
    if (!mxIsUint32(prhs[1])) {
        mexErrMsgTxt("WindowStartsDates must be uint32.");
    }
    if (!mxIsUint32(prhs[2])) {
        mexErrMsgTxt("WindowEndsDates must be uint32.");
    }

    if (mxGetM(prhs[1]) != mxGetM(prhs[2])) {
        mexErrMsgTxt("WindowStartDates must be the same length as WindowEndDates.");
    }    

    // Prepare the output array.
    ismember = mxCreateLogicalArray(mxGetNumberOfDimensions(prhs[1]), mxGetDimensions(prhs[1]));

    CalculationRoutine(mxGetLogicals(ismember),mxGetData(prhs[0]), 
        mxGetData(prhs[1]), mxGetData(prhs[2]), StartIndex, (int) mxGetM(prhs[1]));

    plhs[0] = ismember;
}

I call it with:

>>InWindow(uint32(5),uint32((1:6)'),uint32((3:8)'))

The code reaches the line between the two mexPrintf calls before the segmentation fault (ie the first call prints, but not the second).

I am on Matlab 2007a (yes, I know), Win7 64bit and VS 2008.


You need to initialize the pointer StartIndex - you're "lucky" that it works the first time, since it is not pointing to a defined memory location. Why not do something more like:

unsigned int StartIndex;
// and either:
StartIndex = 1;
// or:
StartIndex = * (static_cast< unsigned int * >( mxGetData(prhs[3]) ) );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号