2011-04-20 5 views
1

다음 코드는 처음 컴파일시 성공적으로 컴파일되고 올바른 결과를 반환합니다. 두 번째 동일한 호출 만들기, 세그먼트 오류 오류가 발생합니다.Mex 파일에서 두 번째 용도로만 세그먼트 화 오류가 발생합니다.

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

코드가 세그먼트 오류 전에 호출 mexPrintf 둘 사이의 라인 (즉, 첫 번째 호출 인쇄,하지만 두 번째되지 않음)에 도달 :

//% 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 *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; 
} 

나는 그것을 호출합니다.

나는 (그래, 나도 알아)의 MATLAB 2007A에, Win7에 64 비트 및 VS 오전 2008 년 당신은 포인터 StartIndex 초기화 할 필요가

답변

4

- 그것 때문에 당신이 그것을 처음 작동 "운"이야을 정의 된 메모리 위치를 가리 키지 않습니다. 더 좋아하는 무언가를하십시오 :

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

그렇습니다, 그렇습니다. StartIndex를 변수로 사용하고 나서 포인터로 전환하여 업데이트 된 값을 반환 할 수 있었지만 그 부분을 놓쳤습니다. 감사! – MatlabSorter

관련 문제