Is it possible to access a pointer to a structure within another structure via libpointer?

Hello,
I am using an external c/c++ shared library from within MATLAB.
Please see the code below:
% c/c++ interface function
% bool oa_get_pattern_objects ( int instance_id , oa_object_array * pReturned_objects )
function [found object_xyrecords] = get_pattern_objects(instance_id)
oa_object_array.m_objects = {};
oa_object_array.m_size = 0;
pOA_pattern_objects = libpointer('oa_object_array',oa_object_array);
if calllib('OpenAccessCore','oa_get_pattern_objects',instance_id,pOA_pattern_objects) == true
found = true;
proxy_objects = get(pOA_pattern_objects,'Value');
size = proxy_objects.m_size;
%THIS IS ILLEGAL
setdatatype(proxy_objects.m_objects,'oa_xyrecordPtr',1,size)
object_xyrecords = proxy_objects.m_objects.Value;
else
found = false;
object_xyrecords = [];
end
I cannot use setdatatype to inform MATLAB that the field, m_objects, points to an array of structures of the same type.
Perhaps I could get a pointer to the first element in the array and manually convert them in MATLAB one-by-one. Is there a way to do this?
My real desire is to give MATLAB the information it needs and allow it to convert all the elements for me more efficiently and elegantly than I can. Is there a way I can do this?
For a simple c-structure like below is it possible to access the elements in the array pointed to by the pointer, sc_array, by any means in MATLAB?
struct simple_class
{
int my_id;
simple_class * sc_array;
};
Any thoughts are greatly appreciated :-)

 采纳的回答

I've discovered a solution that works but I don't really understand why that is.
I can create records in which a record may contain different structures. If these records are to be initialized in C/C++ then structures must precede any PODs in the record's definition.
struct file
{
char *path_;
}
struct record
{
file f_;
int file_size_; // POD cannot be defined before any struct
}
MATLAB appears to be "reshaping" the memory for structs in MATLAB code.
If a struct is placed after a POD then it's fields are not aligned in memory as I would desire as I cannot access the fields of that struct and confirm the right data is where it should be.
If I define all the structs before PODs then for some reason everything works out okay.
This answer is incomplete as I do not fully understand what is happening or why but instead have only discovered how to circumvent the affects of what is happening so that I can work with MATLAB. It would be wonderful to understand just what IS going on.

4 个评论

What release of MATLAB are you using? There have been alignment bugs with structure layout when there were imbedded structure. I believe they were all fixed by release R2010a.
It may be possible for you to work around this by specifing a structure packing in your header file start with 8 but you can try other values:
#pragma pack 8
Walter POD is a c/c++term it stands for "plain old data" but has a much more specific meaning in the specifications.
Thank you Philip. I'm using the "plain old data" definition for POD.
I've been using R2012a.
It may be possible to use #pragma pack 8 to set my alignment boundaries. I had not thought of that. Thank you. I'll try to keep this in mind going forward but I'm still a little weary.
What is MATLAB doing to my memory or should I be asking how is MATLAB interpreting my memory layout?

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by