# Unsafe\_wrap with array of arrays

**URL:** https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120
**Category:** General Usage
**Created:** [January 3, 2018, 3:16am UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120 "2018-01-03T03:16:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)
#### Post date: [January 3, 2018, 3:16am UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/1 "2018-01-03T03:16:56Z")

</div>

I have data (originally pulled in from python via pycall) that is naturally stored in a 2D array (N, 3) (i…e, it is an array of points in 3D). Right now, I can wrap this in Julia with the command:

```julia
X = unsafe_wrap(Array{Float64,2}, Ptr{Float64}(Xptr), (3,N))

```

What I would like to do is have this wrapped as an array of arrays, so that I can easily just iterate through each of the points. Is this possible? How so?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [January 3, 2018, 3:28am UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/2 "2018-01-03T03:28:17Z")

</div>

You could reinterpret the data as a vector of SVectors from StaticArrays:

```julia
using StaticArrays
julia> x = [1 2 3; 4 5 6]
2×3 Array{Int64,2}:
 1 2 3
 4 5 6

julia> reinterpret(SVector{2, Int}, x, (size(x, 2),))
3-element Array{SVector{2,Int64},1}:
 [1, 4]
 [2, 5]
 [3, 6]

```

Alternatively, you could create a `view` of each column of the array and then store those views in a Vector.

---

<div class="post-metadata">

### Author: ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)
#### Post date: [January 3, 2018, 3:36am UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/3 "2018-01-03T03:36:58Z")

</div>

Does reinterpreting copy the data, or just change the indexing mechanism? I want the latter.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [January 3, 2018, 4:10am UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/4 "2018-01-03T04:10:09Z")

</div>

It just creates a new interpretation of the same data.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 3, 2018, 4:28pm UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/5 "2018-01-03T16:28:16Z")

</div>

Note that on 0.7 this will create a `ReinterpretedArray` which unfortunately has overhead in some cases: [https://github.com/JuliaLang/julia/issues/25014](https://github.com/JuliaLang/julia/issues/25014)

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [January 3, 2018, 8:56pm UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/6 "2018-01-03T20:56:09Z")

</div>

> [@gideonsimpson](#):
>
> X = unsafe\_wrap(Array{Float64,2}, Ptr{Float64}(Xptr), (3,N))

What about just doing this?

```julia
using StaticArrays

X = unsafe_wrap(Array{SVector{3,Float64},1}, Ptr{SVector{3,Float64}}(Xptr), (N,))

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 3, 2018, 8:58pm UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/7 "2018-01-03T20:58:47Z")

</div>

But that has the same problem as described in [ANN: InplaceRealFFT.jl : inplace real-to-complex and complex-to-real FFT - #9 by yuyichao](https://discourse.julialang.org/t/ann-inplacerealfft-jl-inplace-real-to-complex-and-complex-to-real-fft/8116/9)?

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [January 3, 2018, 9:15pm UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/8 "2018-01-03T21:15:19Z")

</div>

No, the memory was allocated by python and will be freed by python. gideonsimpson needs to deal with this problem anyway. Or did I misunderstand the context?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 3, 2018, 9:24pm UTC](https://discourse.julialang.org/t/unsafe-wrap-with-array-of-arrays/8120/9 "2018-01-03T21:24:04Z")

</div>

I missed the whole pycall thing…
