correct use of pointer arithmetic with lib.pointer

조회 수: 7 (최근 30일)
Igor Dimitrijevic
Igor Dimitrijevic 2017년 7월 7일
댓글: Philip Borghesani 2017년 7월 10일
Having read https://fr.mathworks.com/help/matlab/ref/lib.pointer.plus.html, I am doing some tests with pointer arithmetic.
First script:
clear
clc
p = libpointer;
setdatatype(p, 'int32Ptr');
p.Value = [0 0 0 0 0 0];
p2 = p + 3;
p2.Value(1) = 2;
disp(['p = ', mat2str(p.Value)]);
disp(['p2 = ', mat2str(p2.Value)]);
Second script:
clear
clc
p = libpointer;
setdatatype(p, 'int32Ptr');
p.Value = [0 0 0 0 0 0];
p2 = p + 3;
p.Value(1) = 1;
disp(['p = ', mat2str(p.Value)]);
disp(['p2 = ', mat2str(p2.Value)]);
The first script seems to always produce the (expected) output:
p = [0 0 0 2 0 0]
p2 = [2 0 0]
The second one, however, doesn't always produce the (expected) output which is
p = [1 0 0 0 0 0]
p2 = [0 0 0]
Sometimes, p2.Value will have what seems garbage values in it, as shown here:
p = [1 0 0 0 0 0]
p2 = [0 256035536 0]
The random results of script 2 make me suspect script 1 might not be reliable as well...
Is there something wrong in my scripts?

채택된 답변

Philip Borghesani
Philip Borghesani 2017년 7월 7일
편집: Philip Borghesani 2017년 7월 7일
Assigning a new value to the base (original) pointer of a user created libpointer invalidates all pointers created from it. If the libpointer was returned from an external function (MATLAB does not own the memory pointed to) then modifications of the value will be inplace and derived pointers are not invalidated. Try the following code (I prefer creating the pointer in one step.)
p = libpointer('int32Ptr',[0 0 0 0 0 0]);
pa=p+0; %pa=p does not do the job both pointers reference the exact same object
p2 = p + 3;
pa.Value(4) = 3;
disp(['p = ', mat2str(p.Value)]);
disp(['p2 = ', mat2str(p2.Value)]);
I hope this is an experiment for use with loadlibrary/calllib. Otherwise I am curious what your use case is. There should be no need for libponters when not interfacing to a shared library.
  댓글 수: 2
Igor Dimitrijevic
Igor Dimitrijevic 2017년 7월 10일
Thanks for this quick and informative anwser.
You guessed right, this is indeed an experiment for use with loadlibrary/calllib, with the buffer held by the library (outside Matlab).
From what you said, I have the feeling that libpointers to external memory behave very like raw pointers (like C pointers).
Could you please confirm that, for any such "external" libpointer (like those returned by allocateStruct and getListOfStrings in the shrlibsample), and for any other libpointer obtained by assignement or the plus operator from such a libpointer, any read/whrite of the actual Value will be in place in the external memory, with no overhead, with both p.Value and p.Value(a, b)?
Philip Borghesani
Philip Borghesani 2017년 7월 10일
That is correct. Assignment to such pointers modifies the data in place. Reading value does make a copy into a MATLAB variable. There is minimal support for lists of strings you may need a mex or c functions customized for your situation if the api you are using makes extensive use of that sort of data structure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call C from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by