calling with calllib a c function wich returns a pointer of pointers

12 visualizzazioni (ultimi 30 giorni)
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 Commenti
James Tursa
James Tursa il 4 Giu 2020
Modificato: James Tursa il 4 Giu 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
Tommy Vasek il 4 Giu 2020
You're right, I find a easier way for the real situation. Actually, I have a time serie (financial price), and my function compute three indicators (hurst, volatility, residual), so I have three new time series. Actually, I find easier with matlab to change my C code, so now, if my new time series have length L, instead of having an 2D array of shape (3,L), I have an array 1D of shape 3xL.

Accedi per commentare.

Risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by