calling with calllib a c function wich returns a pointer of pointers
Show older comments
After first easy test with calllib, I'm working on the next step, quite close of what I need in real situation. I worte a C function wich returns an pointer of pointer. It's something wich looks like a bit as a computation of a vandermonde matrix.
Here is the test_pp.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "test_pp.h"
double** almost_vandermonde( double* a, int na, int ns )
// na is the length of a
// ns is the number of lines we want in the matrix
{
double** sortie = malloc( ns * sizeof(double) ) ;
for (int i = 0 ; i < ns ; i++ )
{
sortie[i] = malloc( na * sizeof(double)) ;
for (int j = 0 ; j < na ; j++ )
{
sortie[i][j] = pow(a[j],i) ;
}
}
return sortie ;
}
and the test_pp.h
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double** almost_vandermonde( double* a, int na, int ns ) ;
I compile it using gcc
gcc -c test_pp.c -o test_pp.o
gcc -o test_pp.so -shared test_pp.o
Now I try it in matlab
loadlibrary("test_pp","test_pp.h")
b = libpointer('doublePtr',[2 3 4 ]);
na = int32(3) ;
ns = int32(4) ;
s = calllib("test_pp","almost_vandermonde",b,na,ns)
And now s is a libpointer of datatype doublePtr. I would like to have a pointer of pointers. If I try to access to the value in s, by setting a size, it seems that I can only have access to a small, basically the first array with only the one inside ( [2 3 4]^ 0 ).
2 Comments
James Tursa
on 4 Jun 2020
Edited: James Tursa
on 4 Jun 2020
Can you tell us what the real situation is? I am not a fan of creating an array of allocated pointers to mimic a 2D matrix. It is simpler to just allocate a single block of memory and address into that. I might propose you so this instead, but it may not be appropriate for what you are "really" doing, so I would like to know that first.
Tommy Vasek
on 4 Jun 2020
Answers (0)
Categories
Find more on Dates and Time 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!