Reading bytes from a pointer passed to C via ccall in Julia

Hello…
I have a C function that has a pointer as one of the parameters. The function structure looks as follows:

int fn(..., void** result){ // body... *result = some_bytes; // body... }

In Julia, part of the ccall parameter is Ptr{Ptr{Void}} that matches the result argument in the function above.
This result argument is actually modified by the C funtion.To represent the result argument in Julia, I use a variable, say julia_result = Ref{Ptr{Void}}(C_NULL). I’ve also trued removing the C_NULL there. After executing the ccall , all other parameters seem to work as expected but unsafe_load(julia_result[]) always returns nothing. Given the output of the function, I would expect that the julia_result variable would also contain some data.What would be the appropriate way to pass this variable in order to read the value written onto it? Thanks

That’s because it’s a point to void. What is the actual type and size of the result being allocated?

1 Like

I’m actually writing binary data/a bytes to the result. There’s another variable that tracks the allocated size and it returns it to the Julia function; this seems to work.

Essentially, the assigned bytes are a serialized proto.

So what you want in Julia is an array of UInt8, and you know the length len? Then the easiest thing to do is:

a = unsafe_wrap(Ptr{UInt8}(julia_result[]), len)

to get a Julia Vector{UInt8} that you can access normally. (However, you should be careful about who is in charge of de-allocating the array, and when it will be freed.)

Alternatively, you can convert the pointer to Ptr{UInt8} and just use unsafe_load to access individual bytes.