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

4 visualizaciones (últimos 30 días)
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;

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 12 de Ag. de 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.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Productos


Versión

R2008a

Community Treasure Hunt

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

Start Hunting!

Translated by