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
MathWorks Support Team 2009년 8월 12일
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