# Embedding julia in C, Segmentation fault when call hcat to concatenate two arrays

**URL:** https://discourse.julialang.org/t/embedding-julia-in-c-segmentation-fault-when-call-hcat-to-concatenate-two-arrays/54597
**Category:** General Usage
**Tags:** embedding
**Created:** [February 4, 2021, 7:22am UTC](https://discourse.julialang.org/t/embedding-julia-in-c-segmentation-fault-when-call-hcat-to-concatenate-two-arrays/54597 "2021-02-04T07:22:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fanxp](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@fanxp](https://discourse.julialang.org/u/fanxp)
#### Post date: [February 4, 2021, 7:22am UTC](https://discourse.julialang.org/t/embedding-julia-in-c-segmentation-fault-when-call-hcat-to-concatenate-two-arrays/54597/1 "2021-02-04T07:22:23Z")

</div>

Hi,  
I need to call some julia function in C, but meet some problem. For example, I want to use `hcat` to concatenate two arrays, but segmentation fault occurs. Below is my source code.

```nohighlight
#include <stdlib.h>
#include <julia.h>

jl_value_t * checked_eval_string(const char* code)
{
    jl_value_t * result = jl_eval_string(code);
    if (jl_exception_occurred()) {
        // none of these allocate, so a gc-root (JL_GC_PUSH) is not necessary
        jl_call2(jl_get_function(jl_base_module, "showerror"),
                 jl_stderr_obj(),
                 jl_exception_occurred());
        jl_printf(jl_stderr_stream(), "\n");
        jl_atexit_hook(1);
        exit(1);
    }
    assert(result && "Missing return value but no exception occurred!");
    return result;
}

int main(void) {
    jl_init();
    // gc protect
    jl_value_t * refs = jl_eval_string("refs = IdDict()");
    jl_function_t * setindex = jl_get_function(jl_base_module, "setindex!");

    double * existing_a = (double *)malloc(sizeof(double) * 4);
    existing_a[0] = 1;
    existing_a[1] = 2;
    existing_a[2] = 3;
    existing_a[3] = 4;
    jl_value_t * array_type = jl_apply_array_type((jl_value_t *)jl_float64_type, 1);
    jl_array_t * a = jl_ptr_to_array_1d(array_type, existing_a, 4, 0);
    jl_call3(setindex, refs, (jl_value_t *)a, (jl_value_t *)a);

    double * existing_b = (double *)malloc(sizeof(double) * 2);
    existing_b[0] = 5;
    existing_b[1] = 6;
    jl_array_t * b = jl_ptr_to_array_1d(array_type, existing_b, 2, 0);
    jl_call3(setindex, refs, (jl_value_t *)b, (jl_value_t *)b);

    jl_function_t * print = jl_get_function(jl_base_module, "show");
    jl_function_t * hcat = jl_get_function(jl_base_module, "hcat");

    jl_call1(print, (jl_value_t *)a);
    printf("\n");
    jl_call1(print, (jl_value_t *)b);
    printf("\n");

    jl_value_t * c = jl_call2(hcat, (jl_value_t *)a, (jl_value_t *)b);
    jl_call3(setindex, refs, c, c);
    jl_call1(print, c);
    printf("\n");

    jl_atexit_hook(0);
    free(existing_a);
    existing_a = NULL;
    free(existing_b);
    existing_b = NULL;
    return 0;
}

```

And the output is:

```julia
[1.0, 2.0, 3.0, 4.0]
[5.0, 6.0]

signal (11): Segmentation fault
in expression starting at none:0
sig_match_simple at /buildworker/worker/package_linux64/build/src/typemap.c:128 [inlined]
jl_typemap_entry_assoc_exact at /buildworker/worker/package_linux64/build/src/typemap.c:662
jl_typemap_assoc_exact at /buildworker/worker/package_linux64/build/src/julia_internal.h:1074 [inlined]
jl_typemap_level_assoc_exact at /buildworker/worker/package_linux64/build/src/typemap.c:715
jl_typemap_assoc_exact at /buildworker/worker/package_linux64/build/src/julia_internal.h:1078 [inlined]
jl_lookup_generic_ at /buildworker/worker/package_linux64/build/src/gf.c:2342 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2394
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1690 [inlined]
jl_call3 at /buildworker/worker/package_linux64/build/src/jlapi.c:269
main at /home/fanxp/vscode/julia_test/main2.cpp:50
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_start at ./build/julia_test (unknown line)
Allocations: 2546 (Pool: 2536; Big: 10); GC: 0
Segmentation fault

```

The expected output is:

```julia
[1.0, 2.0, 3.0, 4.0]
[5.0, 6.0]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

```

My environment is:

```julia
ubuntu 18.04
julia v1.5.3
gcc v7.5.0

```

The source code I provided uses `hcat` function in julia to concatenate 1d array `a` and `b`. I have already allocated the array and initialized them with some data, so I use`jl_ptr_to_array_1d` to wrap them. According to the output, array `a` and `b` are created successfully, but fail to call `hcat`. After exhaustive online searching/reading on google, I find nothing. Anyone can help me?  
Thanks!

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [February 4, 2021, 7:49am UTC](https://discourse.julialang.org/t/embedding-julia-in-c-segmentation-fault-when-call-hcat-to-concatenate-two-arrays/54597/2 "2021-02-04T07:49:09Z")

</div>

Since Julia’s arrays are column-major (1D arrays are column vectors), you probably want to use `vcat`.

```julia
julia> hcat([1, 2, 3, 4], [5, 6])
ERROR: DimensionMismatch("vectors must have same lengths")
Stacktrace:
 [1] hcat(::Vector{Int64}, ::Vector{Int64})
   @ Base .\array.jl:1710
 [2] top-level scope
   @ REPL[45]:1

julia> vcat([1, 2, 3, 4], [5, 6])
6-element Vector{Int64}:
 1
 2
 3
 4
 5
 6

```

---

<div class="post-metadata">

### Author: ![fanxp](https://avatars.discourse-cdn.com/v4/letter/f/b487fb/32.png) [@fanxp](https://discourse.julialang.org/u/fanxp)
#### Post date: [February 4, 2021, 8:07am UTC](https://discourse.julialang.org/t/embedding-julia-in-c-segmentation-fault-when-call-hcat-to-concatenate-two-arrays/54597/3 "2021-02-04T08:07:47Z")

</div>

Thank you very much. I should check my source code carefully.
