# Checking if a vector of structs is contiguous in memory

**URL:** <https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 19, 2017, 2:49pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759 "2017-03-19T14:49:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vipstar](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@vipstar](https://discourse.julialang.org/u/vipstar)\
**Post date:** [March 19, 2017, 2:49pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/1 "2017-03-19T14:49:11Z")

</div>

Hi,

So I have an vector of tuples of floats, i.e.: `Array{Tuple{Float64,Float64},1}()`. I know that tuples are immutables and so theoretically this array should be contiguous in memory and there should be no pointers in the vector or the tuples. Is this true? And more generally is there a way that I can check whether is type in contiguous in memory or whether it contains pointers in it?

Thanks.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [March 19, 2017, 3:00pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/2 "2017-03-19T15:00:00Z")

</div>

You could try something like that:

```julia
x = Array{Tuple{Float64,Float64},1}()
push!(x,(1.,1.),(2.,2.),(3.,3.))

ptr = pointer(x)
offset = sizeof(x[1])
unsafe_load(ptr)
unsafe_load(ptr+offset)
unsafe_load(ptr+2offset)

```

---

<div class="post-metadata">

**Author:** ![m-j-w](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-j-w/32/4541_2.png) [@m-j-w](https://discourse.julialang.org/u/m-j-w)\
**Post date:** [March 19, 2017, 3:55pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/3 "2017-03-19T15:55:07Z")

</div>

You can get a pointer to the n-th element of a vector `v` using `pointer(v,n)`. The difference between two consecutive indices gives accordingly the size occupied by one element in the vector. You can then compare that to the actual size of the elements of `v`:

```nohighlight
v = Vector( [(1., 2., 3.), (4., 5., 6.), (7., 8., 9.)] )
d = Signed(pointer(v,2) - pointer(v,1)) # = 24
d == sizeof(eltype(v)) ? "contiguous" : "non-contiguous or padded"
# prints "contiguous"

```

If you work with `Array` then the above should always hold if `eltype(v)` is a bitstype; check with `Base.isbits(eltype(v))`. For other element types or `AbstractArray` in general, it is not necessarily the case.  
Also, if `v` were a `SubArray`, you could directly use `Base.iscontiguous(v)`, which unfortunately is not implemented for plain `Array` types…

```nohighlight
s = SubArray( v, (1:3,)) # make a subarray from `v`
Base.iscontiguous(s) # returns true

```

---

<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:** [March 19, 2017, 4:04pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/4 "2017-03-19T16:04:55Z")

</div>

> [@m-j-w](#):
>
> Base.iscontiguous(v)

This does not tell you if there’s indirection.

If you want to programatically check if an `Array` has no indirection, `isbits(eltype(v))` should tell you. If you want to verify/see it yourself, you can use the `unsafe_load` in @jonathanBieler’s answer to check. Note that `d == sizeof(eltype(v))` is wrong since it gives your wrong answer for mutable struct of the right size.

---

<div class="post-metadata">

**Author:** ![m-j-w](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-j-w/32/4541_2.png) [@m-j-w](https://discourse.julialang.org/u/m-j-w)\
**Post date:** [March 19, 2017, 4:11pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/5 "2017-03-19T16:11:34Z")

</div>

True. Further, `d == sizeof(eltype(v))` would give a false positive if `eltype(v)` were mutable and `sizeof(eltype(v))` were of size of a pointer… A generalization of `Base.iscontigous` might indeed be quite useful.

---

<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:** [March 19, 2017, 4:12pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/6 "2017-03-19T16:12:20Z")

</div>

`Base.iscontiguous` is not meant for this. It’s only for indexing.

---

<div class="post-metadata">

**Author:** ![vipstar](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@vipstar](https://discourse.julialang.org/u/vipstar)\
**Post date:** [March 19, 2017, 4:22pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/7 "2017-03-19T16:22:58Z")

</div>

Looks like `isbits(eltype(x))` works for this particular case and the `unsafe_load` method works in general. Thanks for the prompt answers everyone.

---

<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:** [March 19, 2017, 4:32pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/8 "2017-03-19T16:32:03Z")

</div>

> [@vipstar](#):
>
> unsafe\_load method works in general.

Just to be clear that you should not use this in real code. You can get a segfault if you made the wrong guess.

---

<div class="post-metadata">

**Author:** ![vipstar](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@vipstar](https://discourse.julialang.org/u/vipstar)\
**Post date:** [March 19, 2017, 5:07pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/9 "2017-03-19T17:07:33Z")

</div>

Oh yes I got that. I just wanted to check that this data structure is continguous to make sure it won’t be too slow.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [October 22, 2021, 12:08am UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/10 "2021-10-22T00:08:17Z")

</div>

I love the manually digging through memory approach. And for those who want an easier way, @tkf told me about `Base.allocatedinline` which does precisely this. [When will a vector of structs be contiguous in memory? - #2 by tkf](https://discourse.julialang.org/t/when-will-a-vector-of-structs-be-contiguous-in-memory/70181/2)

---

<div class="post-metadata">

**Author:** ![Jasha10](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasha10/32/7508_2.png) [@Jasha10](https://discourse.julialang.org/u/Jasha10)\
**Post date:** [March 10, 2022, 5:45pm UTC](https://discourse.julialang.org/t/checking-if-a-vector-of-structs-is-contiguous-in-memory/2759/11 "2022-03-10T17:45:34Z")

</div>

If I’m not mistaken, it should be `isbitstype(eltype(x))` instead of `isbits(eltype(x))`.
