mxCreateStructArray and mxCreateStructMatrix field name memory management

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

James Tursa
James Tursa le 8 Déc 2016
Modifié(e) : James Tursa le 12 Jan 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)

Plus de réponses (0)

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by