How to declare a pointer to a pointer array?

23 views (last 30 days)
My code (which is adopted from C) necessitates me to declare a pointer to a pointer array. While it is typically from a C code, I'm trying to incorporate something like that in Matlab. Since this pointer has to be passed as an argument in a function that is from a shared C library, my pointer which essentially points to a Matrix cannot imitate 'pointer of pointer array' behavior. For a better understanding I'll type in the code from both C and Matlab that I have at the moment.
C code:
void* target[max];
for(c=0,c<max,c++)
{
target[c] = malloc(samples*bytes_per_sample);
}
Myfuncfromsharlib(target,....(other arguments)); /** signature of this function being Myfuncfromsharlib( void** , ...) **/
MATLAB code:
target = libpointer('int16Ptr',int16(zeros(samples,2)));
calllib('sharedlib','Myfruncfromsharlib',target,.. (other arguments)... );
By doing this, whenever I have only one element in my pointer array, I have the answer. But when I have two elements, I have only the second one as the output. For this reason, I tried using a structure of pointers, and also pointer arrays, but they return an error.
Error using calllib
Parameter must be a scalar handle
You guys have any ideas?
Thank You!
  3 Comments
RakJuk
RakJuk on 12 Sep 2016
Edited: RakJuk on 12 Sep 2016
I'm trying to acquire data from an external device that has in-built C libraries which can be made use of. Also, let me tell you that these libraries are easier to access from Matlab in Windows OS as they have a wrapper around the libraries, which in turn creates mex files. However, I have to use this set up on Linux. So, my question is the same as what I asked. I have a parsing/data representation problem since I move to matlab, but still use C functions. I hope my question is better now.
Philip Borghesani
Philip Borghesani on 12 Sep 2016
If you can get the source code to the Windows wrapper it should not be difficult to port it to Linux. I expect it does something similar to what I suggested in my answer.

Sign in to comment.

Answers (2)

Philip Borghesani
Philip Borghesani on 9 Sep 2016
Edited: Philip Borghesani on 9 Sep 2016
MATLAB can't create and manage an array of pointers to data buffers for external use. My suggestion is to create a mex file or new shared library that simplifies and helps with the interface to the shared library you are attempting to use. I might start with these functions:
void** create_target(int max, int samples, int bytes_per_sample)
{
void* target[max];
for(c=0,c<max,c++)
{
target[c] = malloc(samples*bytes_per_sample);
}
return target;
}
void * GetScan(void **target, int scan)
{
return target[scan)]
}
% MATLAB code:
%loadlibrarys
target=calllib('mylib','create_target',max,samples , bytes_per_sample)
calllib('sharedlib','Myfruncfromsharlib',target,.. (other arguments)... );
for n=1:num_scans
scans(n)=calllib('mylib','GetScan',target,n) % probably want more code here to retrieve pointer value and put scans in matrix or cell array...
end
It may be possible to avoid writing GetScan and in MATLAB use pointer addition:
scanptr=libpointer('int16Ptr',target);
for n=1:num_scans
scanptr=libpointer('int16Ptr',target+n-1);
scanptr.reshape(samples,1);
scans(:,n)=scanptr.value;
end

Thorsten
Thorsten on 9 Sep 2016
Edited: Thorsten on 9 Sep 2016
Hm. I think it would be best to use genuine Matlab to solve your problem, instead of fiddling with pointers. I'd like to do that when using C, but have never used pointers in Matlab. If you could say what you want to do, there might me a Matlab one-liner to achieve it. Otherwise I fear that we run into an XY problem.
For example, to allocate an array of size samples*bytes_per_sample x max, you can use
target(samples*bytes_per_sample, cmax) = 0;
and you can store Matlab indices in target; indices correspond pretty much t pointers.

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!