# How can you insert a julia struct into an array of a type in fortran?

**URL:** <https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 30, 2023, 3:06pm UTC](https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775 "2023-01-30T15:06:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 30, 2023, 3:06pm UTC](https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775/1 "2023-01-30T15:06:54Z")

</div>

I am currently trying to create a unittest for a function I have defined in my shared fortran library. This function receives an integer value and inserts it into a previously allocated and declared array of a specific fortran type (the equivalent to a c++ struct in fortran)

My julia code looks like this

```julia
using Base.Libc.Libdl

mutable struct jul_param
    M::Float64
    dot_gamma_0::Float64
    dilatation::Bool
    output::String
end

try
    damask_bin = Libdl.dlopen("/path/to/my/shared_lib.so")
    function_sym = Libdl.dlsym(damask_bin, Symbol("__modnamestart.testmodule_MOD_test_func"))
    param_sym = Libdl.dlsym(damask_bin, Symbol("__modnamestart.testmodule_MOD_test_param"))
    param_pointer = cglobal(param_sym, Ptr{jul_param})
    testparam = jul_param(1.0, 2.0, true, "output1")
    ptr = ccall((:malloc, "/usr/lib/x86_64-linux-gnu/libc.so.6"), Ptr{jul_param}, (Csize_t,), sizeof(jul_param) * 2)
    unsafe_store!(ptr,testparam,1)
    println(unsafe_load(ptr,1)) #testparam(1.0, 2.0, true, "output1")

    err = ccall(function_sym, 
                Cvoid, 
                (Ref{Int64},Ref{Int64}), 
                1,2)
catch e
    println("Error: ", e)
end

```

And my Fortran code currently looks like this:

```julia
module testmodule

  type :: ParamsTest
    real(pReal) :: &
      M, &
      dot_gamma_0
    logical :: &
      dilatation
    character(len=pStringLen), allocatable, dimension(:) :: &
      output
  end type ParamsTest

  type(ParamsTest), allocatable, dimension(:) :: test_param

  contains

  module subroutine test_func(index, test)
    integer, intent(in) :: &
      index, test
    print *, "input", index, test
    print *, "array", test_param(ph)%output
  end subroutine test_func

end module testmodule

```

However, this results in a segmentation fault in the fortran line `print *, "array", test_param(ph)%output`. I am currently using malloc from libc to allocate memory for my struct, is the problem possibly related to this? How can I fix my julia function to accomplish what I want?

---

<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:** [January 30, 2023, 4:42pm UTC](https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775/2 "2023-01-30T16:42:09Z")

</div>

> [@dmmpie](#):
>
> `param_pointer = cglobal(param_sym, Ptr{jul_param})`

One possible here: the type should probably just be `jul_param` because returning a pointer to the global is part of what `cglobal` does. (Btw, it’s conventional to capitalized types in Julia.) I don’t really understand the Fortran code or how and where the global with that structure is being allocated though.

---

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 30, 2023, 5:20pm UTC](https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775/3 "2023-01-30T17:20:36Z")

</div>

changing this line to `param_pointer = cglobal(param_sym, jul_param)` still produces a segmentation fault, is that what you meant?

---

<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:** [January 30, 2023, 5:22pm UTC](https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775/4 "2023-01-30T17:22:26Z")

</div>

That is what I meant. As I said, I don’t really understand what the Fortran code is doing, so it’s hard to give any further help. If you had a comparable example in C, I’d be able to help.

---

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 30, 2023, 5:31pm UTC](https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775/5 "2023-01-30T17:31:54Z")

</div>

It’s really the same as creating a global struct and a global array of that struct in c, and having a function that takes an integer which refers to the index of the global struct array. During this array access a segmentation fault occurs, so I am trying to figure out how to properly assign this global struct array so this doesn’t happen

---

<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:** [February 1, 2023, 2:28pm UTC](https://discourse.julialang.org/t/how-can-you-insert-a-julia-struct-into-an-array-of-a-type-in-fortran/93775/6 "2023-02-01T14:28:30Z")

</div>

Continued here: [How do you correctly align array memory when using unsafe\_store! to write a struct array to a c or fortran program?](https://discourse.julialang.org/t/how-do-you-correctly-align-array-memory-when-using-unsafe-store-to-write-a-struct-array-to-a-c-or-fortran-program/93868)
