# Unsafe\_wrap perform memory allocations

**URL:** <https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662>\
**Category:** General Usage\
**Tags:** array, c\
**Created:** [December 10, 2024, 3:04pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662 "2024-12-10T15:04:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [December 10, 2024, 3:04pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662/1 "2024-12-10T15:04:56Z")

</div>

Hi!  
Im trying to wrap c pointer array into julia as follows:

```julia
function WrapArray(arr :: Ptr{Cdouble}, length :: Cint) :: Cdouble
    @timev wrapped = unsafe_wrap(Vector{Cdouble}, arr, length)   

```

```julia
  0.000003 seconds (2 allocations: 64 bytes)
elapsed time (ns): 2700.0
gc time (ns): 0
bytes allocated: 64

```

just to be able to get values from array.

`But its perform allocations and slow down every function call twice.`

This way is better to write c++ binding for array (CxxWrap) and do cast.  
Which is much much faster. The price is unsupported array operations.

Is there a way to cast pointer into julia array without memory allocation?

I found the following old discussion  
[unsfe\_wrap allocations](https://github.com/JuliaLang/julia/issues/28305?ysclid=m4ijmo6zqn862075523) which propose to make a custom array inside julia. (which i not fully understand yet)

Or reinterpret could work?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 10, 2024, 3:15pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662/2 "2024-12-10T15:15:25Z")

</div>

You can use `UnsafeArray` from [UnsafeArrays.jl](https://github.com/JuliaArrays/UnsafeArrays.jl):

```julia
UnsafeArray(ptr, Int(length))

```

---

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [December 10, 2024, 3:28pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662/3 "2024-12-10T15:28:54Z")

</div>

Thank for a hint  
im trying the following but its not working:

```julia
comp = UnsafeArrays.UnsafeArray{Cdouble, 1}(arr, length)
```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 10, 2024, 3:29pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662/4 "2024-12-10T15:29:57Z")

</div>

You probably need to `Int`ify the size (`length` in your case) like I did in my example (also, it is easier to help if you include error messages).

---

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [December 10, 2024, 3:33pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662/5 "2024-12-10T15:33:06Z")

</div>

My `length` is `Cint`.  
I try out cast directly as you did and errors still same.

But im using c++ to call julia and have pure messaging (dot get correct way to catch exceptions, `JL_TRY` is strange - it could be used only once)

```julia
fatal: error thrown and no exception handler available.
MethodError(f=UnsafeArrays.UnsafeArray{Float64, 1}, args=(0x000001ef854c8cb0, 3), world=0x00000000000068ac)
jl_method_error_bare at C:/workdir/src\gf.c:2254
jl_method_error at C:/workdir/src\gf.c:2272
jl_lookup_generic_ at C:/workdir/src\gf.c:3106
ijl_apply_generic at C:/workdir/src\gf.c:3121
```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 10, 2024, 3:36pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662/6 "2024-12-10T15:36:30Z")

</div>

Here’s a minimal demo of how to construct an `UnsafeArray` from a `Ptr`:

```julia-repl
julia> using UnsafeArrays

julia> let N = 10, T = Float64
           _ptr = Libc.malloc(sizeof(T) * N)
           ptr = convert(Ptr{T}, _ptr)
           
           A = UnsafeArray(ptr, (N,))
           A .= 1
           @show sum(A)

           Libc.free(ptr)
       end
sum(A) = 10.0

```

the important thing to know here is that you have to call `UnsafeArray(::Ptr{T}, ::NTuple{N, Int})` to construct an `UnsafeArray{T, N}`. For some reason they don’t have a method for `UnsafeArray{T, N}(::Ptr, dims...)`. That’s why I did the `convert` step in the above code.

> [@Sergey](#):
>
> But im using c++ to call julia and have pure messaging (dot get correct way to catch exceptions, `JL_TRY` is strange - it could be used only once)

I’d recommend that at least while debugging your code and learning to make it work, you run your code within julia. It’ll make it easier to get error messages that make sense and get people to help you.

See also [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![Sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Sergey](https://discourse.julialang.org/u/Sergey)\
**Post date:** [December 10, 2024, 3:48pm UTC](https://discourse.julialang.org/t/unsafe-wrap-perform-memory-allocations/123662/7 "2024-12-10T15:48:15Z")

</div>

Thanks for example. I get to know about `let` syntax.

Try to figure out error handling in c++.

Run it in `REPL` - and everything is ok.

In my case its worked only if length `specifyed` in the code

```julia
wrapped = UnsafeArray{Cdouble, 1}(arr, (3,))

```

And its woked perfectly, but i don have length at compile time.
