# Avoid copy when using data for Ptr{T}

**URL:** <https://discourse.julialang.org/t/avoid-copy-when-using-data-for-ptr-t/128602>\
**Category:** General Usage\
**Tags:** question, ccall, interface\
**Created:** [May 1, 2025, 9:08pm UTC](https://discourse.julialang.org/t/avoid-copy-when-using-data-for-ptr-t/128602 "2025-05-01T21:08:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)\
**Post date:** [May 1, 2025, 9:08pm UTC](https://discourse.julialang.org/t/avoid-copy-when-using-data-for-ptr-t/128602/1 "2025-05-01T21:08:50Z")

</div>

Hi,  
I call a fortran code which calls Julia trough a @cfunction using pointers (Ptr{T}), and I want to  
avoid copying data from Fortran in Julia such as in the following code

```julia
using Libdl:dlopen, dlsym
using Test

function getff(n, fj)
  function proxy!(n::Cint,x::Ptr{Float64},f::Ptr{Float64})
       xv = zeros(n)
       for i=1:n # <-I WANT TO AVOID THAT COPY
        xv[i] = unsafe_load(x, i)
       end
       fv = fj(xv)
       unsafe_store!(f,fv)
    nothing
  end
  return proxy!
end 

io = open("dummy.f", "w")
prgm="""
           subroutine apply_f(ff,n,x0,f0)
           integer*4 n
           real*8 x0(n)
           external ff                
           call ff(n, x0, f0)
           end subroutine   
     """
write(io,prgm)
close(io)
run(`gfortran -o dummy.so -shared dummy.f`)
n = 2
f(x) = x[1]^2 + x[2]^2
ff = @cfunction($(getff(n, f)), Cvoid, (Ref{Cint}, Ptr{Float64}, Ptr{Float64} ))
test_call = dlopen("./dummy.so")
test_ptr = dlsym(test_call, :apply_f_)
x0 = [1., 2.]
f0 = [1.]
ccall(test_ptr, Cvoid, (Ptr{Cvoid}, Ref{Cint}, Ptr{Float64}, Ptr{Float64}), ff, n, x0, f0)
@test f0[1] ≈ f(x0) atol=1e-30
run(`rm dummy.so dummy.f`)

```

The idea is to wrap directly a Vector{Float64} on my Ptr{Float64}, but I do not know how to do that

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 2, 2025, 1:52am UTC](https://discourse.julialang.org/t/avoid-copy-when-using-data-for-ptr-t/128602/2 "2025-05-02T01:52:40Z")

</div>

> [@aitzkora](#):
>
> The idea is to wrap directly a Vector{Float64} on my Ptr{Float64}, but I do not know how to do that

[`unsafe_wrap`](https://docs.julialang.org/en/v1/base/c/#Base.unsafe_wrap-Union%7BTuple%7BN%7D,%20Tuple%7BT%7D,%20Tuple%7BUnion%7BType%7BArray%7D,%20Type%7BArray%7BT%7D)

---

<div class="post-metadata">

**Author:** ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)\
**Post date:** [May 2, 2025, 7:06am UTC](https://discourse.julialang.org/t/avoid-copy-when-using-data-for-ptr-t/128602/4 "2025-05-02T07:06:50Z")

</div>

Hi Steven,  
thanks effectively `xv = unsafe_wrap(Array, x, n)` do the job 👍. The documentation is not very clear because it documentates only the case with the first argument equals to `Array` . Searching for examples in the sources of Julia gave me uses cases. Bests

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 2, 2025, 12:26pm UTC](https://discourse.julialang.org/t/avoid-copy-when-using-data-for-ptr-t/128602/5 "2025-05-02T12:26:34Z")

</div>

> [@aitzkora](#):
>
> The documentation is not very clear because it documentates only the case with the first argument equals to `Array` .

There are other methods that take `String` or `Memory` as the first argument; it would be good to document those if someone wants to submit a PR.

---

<div class="post-metadata">

**Author:** ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)\
**Post date:** [May 2, 2025, 7:26pm UTC](https://discourse.julialang.org/t/avoid-copy-when-using-data-for-ptr-t/128602/6 "2025-05-02T19:26:25Z")

</div>

I could try to do it even if I am not english native speaker and I never used these two other options . At least, I’ll have a look for other examples in Julia’s source tree
