# Calling C and Fortran Code (Julia documentation)

**URL:** <https://discourse.julialang.org/t/calling-c-and-fortran-code-julia-documentation/23981>\
**Category:** General Usage\
**Created:** [May 7, 2019, 11:57pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code-julia-documentation/23981 "2019-05-07T23:57:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thuener](https://avatars.discourse-cdn.com/v4/letter/t/7ba0ec/32.png) [@Thuener](https://discourse.julialang.org/u/Thuener)\
**Post date:** [May 7, 2019, 11:57pm UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code-julia-documentation/23981/1 "2019-05-07T23:57:28Z")

</div>

I have many questions about Memory Ownership in [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/).

One example, from this documentation we have:

```julia
function sf_bessel_Jn_array(nmin::Integer, nmax::Integer, x::Real)
    if nmax < nmin
        throw(DomainError())
    end
    result_array = Vector{Cdouble}(undef, nmax - nmin + 1)
    errorcode = ccall(
        (:gsl_sf_bessel_Jn_array, :libgsl), # name of C function and library
        Cint, # output type
        (Cint, Cint, Cdouble, Ref{Cdouble}),# tuple of input types
        nmin, nmax, x, result_array # names of Julia variables to pass in
    )
    if errorcode != 0
        error("GSL error code $errorcode")
    end
    return result_array
end

```

In this case Ref{Cdouble} is used and none of the unsafe functions are used. The result\_array is created in Julia and passed to C thus I don’t have to copy anything.

But, String is used in a very different way in gethostname function:

```julia
function gethostname()
    hostname = Vector{UInt8}(undef, 128)
    ccall((:gethostname, "libc"), Int32,
          (Ptr{UInt8}, Csize_t),
          hostname, sizeof(hostname))
    hostname[end] = 0; # ensure null-termination
    return unsafe_string(pointer(hostname))
end

```

In this case one has to use unsafe\_string(pointer(hostname)), but hostname vector was creade in Julia, similar to result\_array. Why I have to copy the memory using unsafe function in this case? Maybe this is done just to truncate C string in NUL-terminated. But If I’m using Fortran I should use unsafe\_string?  
If I change unsafe\_string(pointer(hostname)) to String(hostname) I end up with a String with \0 and some trash.

Also, why use Ptr{UInt8} not Ref{UInt8}? Using Ptr{UInt8} for this case will not implicate in memory leak?  
After sf\_bessel\_Jn\_array function has the following paragraf:

> Note that for this code to work correctly, `result_array` must be declared to be of type `Ref{Cdouble}` and not `Ptr{Cdouble}` . The memory is managed by Julia and the `Ref` signature alerts Julia’s garbage collector to keep managing the memory for `result_array` while the [`ccall`](https://docs.julialang.org/en/v1/base/c/#ccall) executes. If `Ptr{Cdouble}` were used instead, the [`ccall`](https://docs.julialang.org/en/v1/base/c/#ccall) may still work, but Julia’s garbage collector would not be aware that the memory declared for `result_array` is being used by the external C function. As a result, the code may produce a memory leak if `result_array` never gets freed by the garbage collector, or if the garbage collector prematurely frees `result_array` , the C function may end up throwing an invalid memory access exception.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 8, 2019, 12:10am UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code-julia-documentation/23981/2 "2019-05-08T00:10:45Z")

</div>

The implementation of gethostname is wrong. It needs to preserve the hostname array before the string is constructed.

The copying is simply because it was desired to free up the memory. Without copying you are stuck with the original allocation size.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 8, 2019, 12:12am UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code-julia-documentation/23981/3 "2019-05-08T00:12:00Z")

</div>

Also, that paragraph about ptr vs ref for gsl is completely wrong and should be removed immediately. I knew there was some confusing statements in the doc that was causing ppl to misunderstand even though they are correct. I didn’t know this one is there though… This paragraph is just wrong in so many ways and there isn’t a way to interprete it in a way that’s actually correct.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [May 8, 2019, 1:27am UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code-julia-documentation/23981/4 "2019-05-08T01:27:59Z")

</div>

The doc was fixed in this [PR](https://github.com/JuliaLang/julia/pull/31700). Now, that paragraph is rephreased as:

> The C function wrapped returns an integer error code; the results of the actual evaluation of the Bessel J function populate the Julia array `result_array` . This variable is declared as a `Ref{Cdouble}` , since its memory is allocated and managed by Julia. The implicit call to [`Base.cconvert(Ref{Cdouble}, result_array)`](https://docs.julialang.org/en/v1.3-dev/base/c/#Base.cconvert) unpacks the Julia pointer to a Julia array data structure into a form understandable by C.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [May 8, 2019, 3:41am UTC](https://discourse.julialang.org/t/calling-c-and-fortran-code-julia-documentation/23981/5 "2019-05-08T03:41:55Z")

</div>

> [@Thuener](#):
>
> But If I’m using Fortran I should use unsafe\_string?

Unfortunately there is no standard Fortran string ABI. I think gfortran attaches the size of the strings as extra arguments, e.g. see here:

> <https://github.com/JuliaParallel/MPI.jl/blob/3f1aa8f45afb27c96ce4b31cc4b655315591331a/src/mpi-base.jl#L159-L161>
