How can I perform pointer arithmetic on an array of structures returned from a shared library in MATLAB 7.8 (R2009a)?

4 次查看(过去 30 天)
I use LOADLIBRARY, CALLLIB, and LIBPOINTER to obtain a pointer to an array of structures from an external library. I wish to iterate through each element in the array using pointer arithmetic. The following code worked in R2007a:
msgSizePtr = libpointer('int32Ptr', 0); %size of structure in bytes
msgPtr = calllib('TestDLL', 'Get_report_Messages', msgCountPtr, msgSizePtr, rc );
ptr = msgPtr
for j = 1:msgCountPtr.value
ptr.value
ptr = ptr + msgSizePtr.value;
end
When running this same code in MATLAB R2008a or later, I get the following error or a SEGV:
??? Can not offset a pointer to be greater then the size of the data pointed to
Error in ==> testdllprog at 38
ptr = ptr + msgSizePtr.value;

采纳的回答

MathWorks Support Team
There has been a change in how MATLAB handles pointer arithmetic. Starting in R2009a, you no longer have to perform byte arithmetic to increment a pointer; instead, you can use standard pointer arithmetic. In other words, the following code will succeed on R2009a:
msgSizePtr = libpointer('int32Ptr', 0); %size of structure in bytes
msgPtr = calllib('TestDLL', 'Get_report_Messages', msgCountPtr, msgSizePtr, rc );
ptr = msgPtr
for j = 1:msgCountPtr.value
ptr.value
ptr = ptr + 1;
end
In R2008a and R2008b, neither of these approaches will work. There are no workarounds for these versions.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2008a

Community Treasure Hunt

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

Start Hunting!

Translated by