mxCreateStructArray and mxCreateStructMatrix field name memory management

2 visualizaciones (últimos 30 días)
I've been trying to find documentation on how the field names are managed with the structure creating functions in mex. Are the field name character arrays copied before being stored internally. Do the character array pointers need to point to the first character of Matlab managed memory (e.g. initialized via mxMalloc)? The only example I could find online was the phonebook.c, which took the string from another Matlab managed variable.

Respuesta aceptada

James Tursa
James Tursa el 8 de Dic. de 2016
Editada: James Tursa el 12 de En. de 2017
"Are the field name character arrays copied before being stored internally?"
Yes. Internally in the mxArray, the field names are copied to a "fieldname pool" that is managed by MATLAB behind the pi pointer. Not directly behind this pointer, but buried a bit. Where it is buried behind the pi pointer has changed over the years (and I haven't actually checked the latest versions), but the bottom line is it is copied. The char pointers you pass in are not in any way attached to the mxArray struct variable.
"Do the character array pointers need to point to the first character of Matlab managed memory (e.g. initialized via mxMalloc)?"
No. Since the field names are copied into MATLAB managed memory, the source pointers can point to anything (native C/C++ memory, MATLAB managed memory, in the middle of an allocated memory block, etc). Or even a mix of the above. Source doesn't matter since the field name strings are copied.
The API functions that actually attach pointers are:
mxSetPr, mxSetPi, mxSetData, mxSetImagData, mxSetCell, mxSetField, mxSetFieldByNumber, mxSetIr, mxSetJc
(In addition to attaching the pointers, they remove addresses from the garbage collection list and in the case of mxArrays change variable types from temporary to sub-element)

Más respuestas (0)

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by