# Creating an Array{Array{UInt8}} in C

**URL:** <https://discourse.julialang.org/t/creating-an-array-array-uint8-in-c/12552>\
**Category:** Internals & Design\
**Tags:** embedding\
**Created:** [July 21, 2018, 6:35am UTC](https://discourse.julialang.org/t/creating-an-array-array-uint8-in-c/12552 "2018-07-21T06:35:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 21, 2018, 6:35am UTC](https://discourse.julialang.org/t/creating-an-array-array-uint8-in-c/12552/1 "2018-07-21T06:35:21Z")

</div>

I want to create an Array{Array{UInt8}} in C and wondered if I got all the garbage collection and write barriers right. The code below compiles and works.

```cpp
#include <julia.h>

extern "C" {
  jl_value_t* test_array_array(int i) {
    jl_value_t *t = jl_apply_array_type(jl_apply_array_type((jl_value_t*)jl_uint8_type, 1), 1);
    jl_value_t *a = (jl_value_t*)jl_alloc_array_1d(t, i);
    JL_GC_PUSH1(&a);

    jl_array_t **d = (jl_array_t**)jl_array_data(a);

    for (int j = 0; j < i; j++) {
      jl_array_t *aa = jl_alloc_array_1d(jl_array_uint8_type, i);
      JL_GC_PUSH1(&aa);

      unsigned char *dd = (unsigned char*)jl_array_data(aa);

      for (int k = 0; k < i; k++) {
        dd[k] = (unsigned char)k;
      }

      d[j] = aa;
      jl_gc_wb(a, aa);

      JL_GC_POP(); //aa
    }

    JL_GC_POP(); //a

    return a;
  }
}

```

Call it with

```julia
g() = ccall((:test_array_array, LIB_PATH),
            Array{Array{UInt8}}, (Cint,), 5)

```

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 21, 2018, 6:46am UTC](https://discourse.julialang.org/t/creating-an-array-array-uint8-in-c/12552/2 "2018-07-21T06:46:55Z")

</div>

On a general note: What is the trade-off between returning Julia objects directly from C, as above, and writing a general C interface and calling that from Julia — besides the obvious that the latter can be used by other programming languages.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 21, 2018, 1:38pm UTC](https://discourse.julialang.org/t/creating-an-array-array-uint8-in-c/12552/3 "2018-07-21T13:38:24Z")

</div>

Writing a general C interface and calling that from Julia is the recommended approach. The other way requires writing and recompiling C code just to update the interface between Julia and C and depends on Julia internals which are not guaranteed to be stable. The ccall approach on the other hand is guaranteed to continue working.

---

<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:** [July 21, 2018, 4:05pm UTC](https://discourse.julialang.org/t/creating-an-array-array-uint8-in-c/12552/4 "2018-07-21T16:05:11Z")

</div>

> [@gdkrmr](#):
>
> JL\_GC\_PUSH1(&aa);

This is not needed.

Otherwise looks correct.

> [@gdkrmr](#):
>
> On a general note: What is the trade-off between returning Julia objects directly from C, as above, and writing a general C interface and calling that from Julia — besides the obvious that the latter can be used by other programming languages.

The question is why do you want to write the code in C. I’ve only seen two cases where this happens,

1. You want to use some C functions that’s not available in julia.

2. You want to share code with other project / languages.

In general, I think in most cases having a julia specific interface will be harder to implement (the code you have above is much shorter with same/better performance in pure julia for example). If you find it simpler to interact with julia in C for the specific usecase you should feel free to do so. In these cases, it’ll be interesting to see why you couldn’t do that in nearly pure julia combined with `ccall` and `llvmcall` though and if such function actually makes sense to be included in julia.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [July 21, 2018, 4:58pm UTC](https://discourse.julialang.org/t/creating-an-array-array-uint8-in-c/12552/5 "2018-07-21T16:58:05Z")

</div>

Thanks for reading through the code. It is to interface some c++ code with julia that does not come with its own compiled library. I wanted to test the effort writing a general c interface and a julia interface. The julia part was a fun exercise to learn some of the internals but in the end the code for the c interface + the julia function to call it is shorter, easier to understand and more performant and guaranteed to be stable.
