MEX application: integer value is kept in memory between runs

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