Prev: OSIsoft's PI
Next: Solve system with ode15s
From: James Tursa on 3 Aug 2010 15:02 "Balint Takacs" <b.takacs(a)imperial.ac.uk> wrote in message <i39m02$jou$1(a)fred.mathworks.com>... > Does anybody know whether mxGetCell() allocates memory, that is, its output mxArray* should be released by mxDestroy() after its use? > > The documentation says that it is possible that the function fails because 'Insufficient free heap space to hold the returned cell mxArray'. However, it looks a bit weird and inefficient that a deep copy of the cell element is made upon simply accessing it. mxGetCell returns the pointer to the original mxArray in the cell, it does *not* allocate memory and create a deep copy of the cell contents. You can verify this with the following mex file: // cellpr.c // input is a cell array, prints the address of the first cell // no output #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mxArray **p; p = mxGetData(prhs[0]); mexPrintf("Raw 1st cell address = %p\n",*p); mexPrintf("mxGetCell 1st address = %p\n",mxGetCell(prhs[0],0)); } >> mex cellpr.c >> c = {1.23} c = [1.2300] >> cellpr(c) Raw 1st cell address = 0B15D0A0 mxGetCell 1st address = 0B15D0A0 You can see that the raw mxArray * address contained in the data area of the cell input prhs[0] is exactly the same as the address returned by the mxGetCell call. No copy is made. The warning in the doc about insufficient heap space causing a failure is probably wrong ... I have no idea why this function would be using any heap space. Same comment would hold for mxGetField and mxGetFieldByNumber for structures. The new mxGetProperty, on the other hand, *does* return a deep copy of the property mxArray and thus uses allocated memory from the heap. James Tursa
|
Pages: 1 Prev: OSIsoft's PI Next: Solve system with ode15s |