Using ccall with pointers in signature

I have a C function with signature double *fwht(double *a, int n). I have compiled the function as part of a shared library and I try to call it with ccall from Julia with

ccall((:fwht, "./libmean.so"), Vector{Float64}, (Vector{Float64}, Int), rand(10_000), 10_000)

However, I get a segmentation fault. I think this is because of the incorrect way I specify types in the ccall function. Could someone tell me how I should define the types in ccall for the signature I have of fwht?

Try

ccall((:fwht, "./libmean.so"), Ptr{Cdouble}, (Ptr{Cdouble}, Cint), rand(10_000), 10_000)
1 Like

It works. But the return type is Ptr{Float64}. How do you recast it into a vector? Or maybe I should say how do you dereference it?

You want to look into unsafe_wrap (and/or unsafe_load). Notice that the “unsafe” part of the name really means that. If you do something sufficiently bad, the program will crash like a C program does under similar circumstances.

1 Like

Okay. The following works:

z_ptr = ccall((:fwht, "./libmean.so"), Ptr{Cdouble}, (Ptr{Cdouble}, Cint), rand(10_000), 10_000)
z = unsafe_wrap(Vector{Float64}, z_ptr, 10_000)