Are `jl_value_t**` compatible with `jl_array_t*`? If not, how do I manage `Any[]` in C?

question

#1

The Docs show the following:

double *existingArray = (double*)malloc(sizeof(double)*10);
jl_array_t *x = jl_ptr_to_array_1d(array_type, existingArray, 10, 0);

I thought I could use jl_value_t** instead of double* in this case, but when giving this array to a julia function, I just get random values as the elements (probably because it is interpreting the type information).

But how then, would you manage an Any[] in Julia if there is no type information on each element? Do I need a different approach or is this something that is just not implemented?

Thanks for the help!


#2

Huh, I think I know the issue: I still use float64 as the datatype… I’ll try changing that when I get home


#3

Ey, it does indeed work when using jl_any_type!

jl_value_t* array_type{jl_apply_array_type(
     reinterpret_cast<jl_value_t*>(jl_any_type), dimensions)};
jl_value_t** _arr = new jl_value_t*[_size];
_metadata = jl_ptr_to_array_1d(array_type, _arr, _size, true);

#4

The way you allocate the jl_value_t* array is wrong. jl_ptr_to_array_1d(..., true) only accept malloc allocated pointers.

Also, even though your code doesn’t have any other issue yet, you must not mutate arr after you created metadata unless you also trigger the write barrier.


#5

Really? Are new and malloc different on the inside? I didn’t know. It currently works with new, but I’ll change it just to be safe


#6

Yes. They have to (in general). For POD, there can be some room to be flexible but that’s definitely undefined behavior.


#7

Interesting. I will switch my code then