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

4 views (last 30 days)
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;

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Aug 2009
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.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!