# Subtleties of type and immutable and memory layout when using ccall

**URL:** <https://discourse.julialang.org/t/subtleties-of-type-and-immutable-and-memory-layout-when-using-ccall/1328>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [January 6, 2017, 3:49pm UTC](https://discourse.julialang.org/t/subtleties-of-type-and-immutable-and-memory-layout-when-using-ccall/1328 "2017-01-06T15:49:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [January 6, 2017, 3:49pm UTC](https://discourse.julialang.org/t/subtleties-of-type-and-immutable-and-memory-layout-when-using-ccall/1328/1 "2017-01-06T15:49:10Z")

</div>

I have spent considerable time finding a bug in my code related to memory layout of types and immutables when interfacing with structs in c. I want to summarize here to check my understanding and as it may be useful to others.

**When to use immutable**  
Firstly, I have been calling a code that requests and array of structs (memory owned by Julia). The C code is like

```nohighlight
typedef struct {
  int size;
  void * data;
} bob_t;

int fill_bob(bob_t * bobs, int nbobs);

```

which I would call from Julia using

```julia
immutable bob_t
    size::Cint
    data::Ptr{Void}
end

function fill_bob(size)
    bobs = Array{bob_t,1}(size)
    err = ccall((:fill_bob, library), Cint, (Ref{bob_t}, Cint), bobs, size)
    bobs
end

```

In this case one must use an `immutable`, because C expects the structs to be stored inline inside the array. If I use a `type` for `bob_t` then AFAICT the array `bobs` only holds references to the types, and so the above code doesn’t work. Is that correct? Is there a data structure that stores `types` inline?

**When to use type**  
On the other hand sometimes one needs a longer term reference to a specific `struct` that is shared between Julia and C. e.g.

```nohighlight
typedef struct {
    int size;
    void * data;
} frank_t;

int do_something(frank_t * frank);

```

In Julia I often want to store such a struct inside another type.

```julia
type frank_t
    size::Cint
    data::Ptr{Void}
end

type Frank
    f::frank_t
    data::Array{Uint8,1}
end

function Frank(size)
    data = Array{Uint8,1)(size)
    Frank(frank_t(size, pointer(data, 1)), data)
end

function do_something(frank::Frank)
    err = ccall((:do_something, library), Cint, (Ref{frank_t},), frank.f)
end

```

In this case I keep a Julia reference to the array in `Frank`, which corresponds to the pointer inside of `frank_t`. Then I can call the c functions as necessary to work on frank\_t, but also access the data directly in Julia using the data array field of `Frank`.

In this case it is essential that `frank_t` and `Frank` are `type` and not `immutable`, because otherwise the actual address of `frank_t` changes from call to call. If the C library stores references to `frank_t` internally (as the one I’m using does) then this can lead to subtle bugs.

Does that make sense? Did I miss something obvious?

Thanks!

---

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [January 6, 2017, 4:14pm UTC](https://discourse.julialang.org/t/subtleties-of-type-and-immutable-and-memory-layout-when-using-ccall/1328/2 "2017-01-06T16:14:04Z")

</div>

It seems like the discussion in:

> [@Create a type with pointer to array](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306):
>
> hi all, I’m new to Julia, and still getting used to the Julia-way of doing things. I was wondering if there is anything that would work as a pointer would in C, or as a reference variable in C++? the particular use case I have in mind is as follows. suppose I create a new type, called “Coord”, that will store a particular coordinate system. immutable Coord val::Array{Real} name::AbstractString end and use it to create my coordinate x: x0 = 0:0.05:1 x = Coord(x0, "mycoord") Now I w…

Has ended up with a similar content.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 6, 2017, 5:56pm UTC](https://discourse.julialang.org/t/subtleties-of-type-and-immutable-and-memory-layout-when-using-ccall/1328/3 "2017-01-06T17:56:14Z")

</div>

> [@jtravs](#):
>
> In this case it is essential that frank\_t and Frank are type and not immutable, because otherwise the actual address of frank\_t changes from call to call. If the C library stores references to frank\_t internally (as the one I’m using does) then this can lead to subtle bugs.

Yes, that all seems correct. I would just add that `Ref{frank_t}` is constructible specifically for the purpose that you can use it to get a stable address pointing to any type `frank_t`. You can even use it to point to an offset into an array holding `frank_t` objects, or to C memory declared as `Ptr{frank_t}`.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [January 6, 2017, 7:22pm UTC](https://discourse.julialang.org/t/subtleties-of-type-and-immutable-and-memory-layout-when-using-ccall/1328/4 "2017-01-06T19:22:12Z")

</div>

Regarding inlining, the distinction is not `type` vs `immutable`, but whether `isbits(T)` is true. See [this manual section](http://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/#struct-type-correspondences). A `type` will never be `isbits==true`; an `immutable` may be either true or false, depending on the field types.

edit: also, regarding the question of inlined `type` – i.e. mutable – objects [see here](https://github.com/JuliaLang/julia/issues/11714) for some of the design issues and tradeoffs involved in the choice of demarcation points around mutability.

---

<div class="post-metadata">

**Author:** ![jtravs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jtravs/32/2010_2.png) [@jtravs](https://discourse.julialang.org/u/jtravs)\
**Post date:** [January 6, 2017, 8:39pm UTC](https://discourse.julialang.org/t/subtleties-of-type-and-immutable-and-memory-layout-when-using-ccall/1328/5 "2017-01-06T20:39:22Z")

</div>

Aha, of course `Ref{frank_t}` is the solution. So in my second case, where I thought we must use a `type`, I can instead do:

```julia
immutable frank_t
    size::Cint
    data::Ptr{Void}
end

immutable Frank
    f::Ref{frank_t}
    data::Array{Uint8,1}
end

function Frank(size)
    data = Array{Uint8,1)(size)
    Frank(Ref{frank_t}(frank_t(size, pointer(data, 1))), data)
end

function do_something(frank::Frank)
    err = ccall((:do_something, library), Cint, (Ref{frank_t},), frank.f)
end

```

Which is more explicit and also makes me happier as I prefer `immutable` when dealing with c structs as they are also value types.
