# Create a C struct in Julia without knowing the internals?

**URL:** <https://discourse.julialang.org/t/create-a-c-struct-in-julia-without-knowing-the-internals/14629>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [September 6, 2018, 4:11pm UTC](https://discourse.julialang.org/t/create-a-c-struct-in-julia-without-knowing-the-internals/14629 "2018-09-06T16:11:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)\
**Post date:** [September 6, 2018, 4:11pm UTC](https://discourse.julialang.org/t/create-a-c-struct-in-julia-without-knowing-the-internals/14629/1 "2018-09-06T16:11:02Z")

</div>

I’m using a C library that uses C structs as internal data storage. However, the library assumes that the user will instantiate these structs him/herself, and then call an initialization routine on them. For instance, in the library header:

```
typedef struct{
    mydata data;
} mystruct;
MY_DEF void mystruct_initialize(mystruct* sph);

```

and the user is expected to do the following:

```
mystruct foo;
mystruct_initialize(foo);

```

So… how do I instantiate a mystruct from Julia? I know I could create a Julia struct mirror definition, but I’d rather not. That would require me to know something about the internal structure of the library data types. There doesn’t seem to be any easy way to instantiate a composite C variable type from within Julia.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [September 6, 2018, 4:22pm UTC](https://discourse.julialang.org/t/create-a-c-struct-in-julia-without-knowing-the-internals/14629/2 "2018-09-06T16:22:50Z")

</div>

If you at least know the size of `mystruct`, you could do something like:

```julia
mutable struct MyStruct
    ptr::Ptr{Cvoid}
    function MyStruct()
        foo = new(Libc.malloc(SIZEOF_mystruct))
        finalizer(x->Libc.free(x.ptr), foo)
        ccall(:mystruct_initialize, Cvoid, (Ptr{Void},), foo.ptr)
        return foo
    end
end

```

It might not be quite syntactically correct, but hopefully that gives you the idea at least.

---

<div class="post-metadata">

**Author:** ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)\
**Post date:** [September 6, 2018, 4:30pm UTC](https://discourse.julialang.org/t/create-a-c-struct-in-julia-without-knowing-the-internals/14629/3 "2018-09-06T16:30:54Z")

</div>

Is the resulting allocated memory owned by Julia (and hence garbage collected) or by C?

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [September 6, 2018, 4:34pm UTC](https://discourse.julialang.org/t/create-a-c-struct-in-julia-without-knowing-the-internals/14629/4 "2018-09-06T16:34:05Z")

</div>

The mutable struct `MyStruct` is owned by Julia and will be garbage collected, but the call to `Libc.malloc` is allocating your own (you the user) memory that you need to manage. Luckily, it’s easy in Julia to add the `finalizer(x->Libc.free(x.ptr)` call, which ensures that when the `MyStruct` object goes out of scope and is garbage collected, it will free the underlying malloc-ed memory.
