mxCreateStructArray and mxCreateStructMatrix field name memory management

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

James Tursa
James Tursa il 8 Dic 2016
Modificato: James Tursa il 12 Gen 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)

Più risposte (0)

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by