mxCreateStructArray and mxCreateStructMatrix field name memory management

1 回表示 (過去 30 日間)
Jim Hokanson
Jim Hokanson 2016 年 12 月 8 日
編集済み: James Tursa 2017 年 1 月 12 日
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 Center および File ExchangeWrite 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