# How do I push an array to the garbage collector in C?

**URL:** <https://discourse.julialang.org/t/how-do-i-push-an-array-to-the-garbage-collector-in-c/13942>\
**Category:** General Usage\
**Created:** [August 23, 2018, 2:34pm UTC](https://discourse.julialang.org/t/how-do-i-push-an-array-to-the-garbage-collector-in-c/13942 "2018-08-23T14:34:13Z")\
**Posts on this page:** 5\
**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:** [August 23, 2018, 2:34pm UTC](https://discourse.julialang.org/t/how-do-i-push-an-array-to-the-garbage-collector-in-c/13942/1 "2018-08-23T14:34:13Z")

</div>

So on [https://docs.julialang.org/en/stable/manual/embedding/](https://docs.julialang.org/en/stable/manual/embedding/) it says how to use Julia arrays and the garbage collector in C, but not how to use them together.

Let’s say, I allocated an array in the following manner as it says in the example:

```julia
jl_value_t* array_type = jl_apply_array_type((jl_value_t*)jl_float64_type, 1);
jl_array_t* x = jl_alloc_array_1d(array_type, 10);
double *xData = (double*)jl_array_data(x);

```

If I do not use the array in the next call to any `jl_` function, it will be garbage collected, as evidenced by (a) it not giving me a garbage value with `jl_gc_enable(0)` and (b) it also working fine if I use the array in Julia in the next call.

I could push the array to the garbage collector, but the example code below is not helping me:

```julia
jl_value_t **args;
JL_GC_PUSHARGS(args, 2); // args can now hold 2 `jl_value_t*` objects
args[0] = some_value;
args[1] = some_other_value;
// Do something with args (e.g. call jl_... functions)
JL_GC_POP();

```

This code requires a `jl_value_t**`, but I don’t have such an array. I only have the `array_type`, `x` and `xData`, which are all three of a different type.

So how would I go about pushing the array so that it does not get garbage collected?

---

<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:** [August 23, 2018, 5:14pm UTC](https://discourse.julialang.org/t/how-do-i-push-an-array-to-the-garbage-collector-in-c/13942/2 "2018-08-23T17:14:35Z")

</div>

> [@kim366](#):
>
> If I do not use the array in the next call to any `jl_` function, it will be garbage collected

Even if you do, it could still be collected if you didn’t root it. The `jl_call...` functions are the exceptions to the rule to make it slightly easier to use (though more error-prone I would say).

> [@kim366](#):
>
> which are all three of a different type

You have such an array, it’s `args`. It stores pointers to julia object. Most (all?) `jl_..._t` you have access of are julia object and you just need to cast the pointer. In this case, `array_type` is a leaf type so you don’t necessarily have to root it. You could also use one of `JL_GC_PUSH<n>` to treat a local variable as root instead of explicitly storing the object to the stack frame everytime.

---

<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:** [August 24, 2018, 1:51pm UTC](https://discourse.julialang.org/t/how-do-i-push-an-array-to-the-garbage-collector-in-c/13942/3 "2018-08-24T13:51:36Z")

</div>

Okay, thanks for your help!

So I again have to box my array contents with the boxing functions? Because right now I am just adding them to `xData` without any boxing.

---

<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:** [August 24, 2018, 3:47pm UTC](https://discourse.julialang.org/t/how-do-i-push-an-array-to-the-garbage-collector-in-c/13942/4 "2018-08-24T15:47:45Z")

</div>

> [@kim366](#):
>
> So I again have to box my array contents with the boxing functions?

What do you mean by box your array content? `x` is the box for `xData`. Unless required by the API, you shouldn’t need to box anything.

---

<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:** [August 29, 2018, 12:52pm UTC](https://discourse.julialang.org/t/how-do-i-push-an-array-to-the-garbage-collector-in-c/13942/5 "2018-08-29T12:52:52Z")

</div>

Yes, thank you. I have looked into boxing and I now understand it. Thanks for your help!
