필터 지우기
필터 지우기

MEX application: integer value is kept in memory between runs

조회 수: 1 (최근 30일)
HaMo
HaMo 2019년 1월 30일
답변: Walter Roberson 2019년 1월 30일
If I create the following C file:
#include "mex.h"
unsigned int inputCounter = 0;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])
{
mexPrintf("inputCounter = %d\n", inputCounter);
inputCounter ++;
}
And compile and run it in Matlab with
mex MexIntegerInit.c
MexIntegerInit
MexIntegerInit
MexIntegerInit
I get the following output:
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.
inputCounter = 0
inputCounter = 1
inputCounter = 2
However, if I compile outside of Matlab with the same compiler (MinGW64), and run it as a standalone without re-compiling between runs, the value is not incremented.
Is this undefined behavior?

채택된 답변

Walter Roberson
Walter Roberson 2019년 1월 30일
no the behaviour is defined . When called inside MATLAB it is being called as a subroutine of the MATLAB process which continues to exist after the call. When called standalone each call creates a process , runs, and terminates returning the memory to the operating system .

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by