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

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

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:

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?

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.

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

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.

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

Continued here: How do you correctly align array memory when using unsafe_store! to write a struct array to a c or fortran program?