# Are \`jl\_value\_t\*\*\` compatible with \`jl\_array\_t\*\`? If not, how do I manage \`Any\[\]\` in C?

**URL:** https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390
**Category:** General Usage
**Tags:** question
**Created:** [October 16, 2018, 8:24am UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390 "2018-10-16T08:24:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)
#### Post date: [October 16, 2018, 8:24am UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390/1 "2018-10-16T08:24:23Z")

</div>

The [Docs](https://docs.julialang.org/en/stable/manual/embedding/) show the following:

```julia
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!

---

<div class="post-metadata">

### Author: ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)
#### Post date: [October 16, 2018, 8:25am UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390/2 "2018-10-16T08:25:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)
#### Post date: [October 17, 2018, 6:05am UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390/3 "2018-10-17T06:05:02Z")

</div>

Ey, it does indeed work when using `jl_any_type`!

```nohighlight
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);

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 17, 2018, 3:14pm UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390/4 "2018-10-17T15:14:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)
#### Post date: [October 25, 2018, 9:32pm UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390/5 "2018-10-25T21:32:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 27, 2018, 1:06am UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390/6 "2018-10-27T01:06:50Z")

</div>

> [@kim366](#):
>
> Really? Are new and malloc different on the inside?

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

---

<div class="post-metadata">

### Author: ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)
#### Post date: [October 28, 2018, 2:39pm UTC](https://discourse.julialang.org/t/are-jl-value-t-compatible-with-jl-array-t-if-not-how-do-i-manage-any-in-c/16390/7 "2018-10-28T14:39:27Z")

</div>

Interesting. I will switch my code then
