# How to keep a reference for C structure to avoid GC?

**URL:** <https://discourse.julialang.org/t/how-to-keep-a-reference-for-c-structure-to-avoid-gc/9310>\
**Category:** General Usage\
**Created:** [February 25, 2018, 7:10am UTC](https://discourse.julialang.org/t/how-to-keep-a-reference-for-c-structure-to-avoid-gc/9310 "2018-02-25T07:10:06Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![pedrohnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pedrohnv/32/212503_2.png) [@pedrohnv](https://discourse.julialang.org/u/pedrohnv)\
**Post date:** [April 23, 2018, 6:13pm UTC](https://discourse.julialang.org/t/how-to-keep-a-reference-for-c-structure-to-avoid-gc/9310/19 "2018-04-23T18:13:42Z")

</div>

I could not make sense of this discussion nor the [sample notebook](https://github.com/tk3369/julia-notebooks/blob/master/ccall%20-%20using%20cconvert%20and%20unsafe_convert.ipynb) to adapt to my specific use case.

Any help would be appreciated:

I have a Struct with a fixed size array field. I want to use Julia to build that struct and then call the C code which will do the heavy computation accessing the elements in the struct. The Julia struct does not need to stay in memory after the ccall.

Here is an example:

```julia
//C code
typedef struct {
    double array[3];
} Mystruct;

int access_struct_array(Mystruct* mystruct)
{
    for (int i = 0; i < 3; i++)
    {
        printf("%f\n", mystruct->array[i]);
    }
    return 0;
}

```

Then I use Julia to build the struct and call the C code

```julia
type Mystruct
    array::Array{Float64, 1}
end
mystruct = Mystruct([1.1, 2.1, 3.1]);
ccall(("access_struct", "./shared.so"), Int, (Ref{Mystruct},), mystruct); #undefined behavior

```

Why is the behavior of `ccall` undefined?

I would say it is related to the fact that [Julia arrays are not just pointers](https://discourse.julialang.org/t/pointer-julia-not-modified-after-ccall-to-c-function-which-uses-another-c-function/10455/13). Then, what is the Julia equivalent of a C array?

---

_[View the full topic](https://discourse.julialang.org/t/how-to-keep-a-reference-for-c-structure-to-avoid-gc/9310)._
