Embedding Julia in C -- error regarding jl_array_ptr_1d_push


#1
#include <julia.h>

int main(int argc, char* argv[]) {
  jl_init();
  jl_value_t* array_type_jl = nullptr;
  jl_value_t* array_jl = nullptr;
  jl_value_t* value_jl = nullptr;
  JL_GC_PUSH3(&array_type_jl, &array_jl, &value_jl);

  array_type_jl = jl_apply_array_type(
     reinterpret_cast<jl_value_t*>(jl_float64_type), 1);

 array_jl = reinterpret_cast<jl_value_t*>(jl_alloc_array_1d(array_type_jl, 0));
 value_jl = jl_box_float64(64.0);
 jl_array_ptr_1d_push(reinterpret_cast<jl_array_t*>(array_jl), value_jl);
 jl_function_t* print_func = jl_get_function(jl_base_module, "println");
 jl_call1(print_func, array_jl);
 JL_GC_POP();
 jl_atexit_hook(0);
}

For the code above, I was expecting array_jl to contain a Float64 number 64.0 but it is actually 6.9438e-310. What did I do wrong?


#2

I’m not sure jl_array_ptr_1d_push is applicable here, since this seems to be for an array with element type Any. I use this sequence in my code:

const size_t pos = jl_array_len(m_array);
jl_array_grow_end(m_array, 1);
jl_arrayset(m_array, box(val), pos);

Also, note that you can wrap a C array of doubles using jl_ptr_to_array, which would be a lot simpler in this case.