Jl_ptr_to_array


#1

How should argument data to the function jl_ptr_to_array be given? I know that in the case of Array{Float64,1} it’s just a pointer to double array.
But suppose in the case of Array{Complex{Float64},1}, what should I pass as data? I tried double complex **, it didn’t work.
And also in case of Array{String,1}, I tried passing char **. Even that didn’t work.

Thank you.


#2

Warning: I am definitely not an expert, and haven’t used the embedding API. I probably have just the right amount of knowledge to be dangerous…

But, for what it’s worth, I’m pretty sure that a Complex{Float64} is just laid out in memory as two adjacent Float64s. That should be the same layout as a C struct containing two double elements (a complex, I guess?). As evidence of that:

julia> sizeof(Float64)
8

julia> sizeof(Complex{Float64})
16

So the data stored in a Julia Array{Complex{Float64}, 1} should, I think, just be a complex * in C.


#3

To construct a Vector{String}, I think you’d need to allocate a Vector{String} first of the right size, and then perform either jl_cstr_to_string or jl_pchar_to_string (depending on whether it is nul byte terminated or you have a length) on each element of the C/C++ vector, sticking those into the allocated vector.


#4

Thank you so much. This worked for Strings allocating first and then applying jl_cstr_to_string on every String. But for an Array{Complex{Float64}}, I tried first allocating the Array and then filling them with boxed Complex{Float64}, Julia couldn’t read the values correctly and it is always the same value.


#5

String, although conceptually an immutable, is implemented with a mutable type, and the vector has pointers in it.
Do you have an Array{Complex{Float64}} (which doesn’t say how many dimensions it has), or more likely a Vector or Matrix of Complex{Float64}?
Complex{Float64} is a struct (immutable) and gets allocated as two 64-bit IEEE floating point values, there is no boxing when they are placed in a Array (Vector or Matrix or n-d Array), you simply get
[real part 1, imag part 1, real part 2, imag part 2, …] in memory.


#6

Thank you for replying. I just omitted the dimensions for simplicity, And I was able to create Array{Complex{Float64}}. Yeah, you’re right. I was just able to assign initialised double complex each position in the allocated vector.

Thank you.