mxCreateStructArray and mxCreateStructMatrix field name memory management

1 次查看(过去 30 天)
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.

采纳的回答

James Tursa
James Tursa 2016-12-8
编辑:James Tursa 2017-1-12
"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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by