# Array of pointers to struct vs. Array of struct

**URL:** <https://discourse.julialang.org/t/array-of-pointers-to-struct-vs-array-of-struct/83150>\
**Category:** General Usage\
**Tags:** question, array, arrays, struct\
**Created:** [June 21, 2022, 10:27pm UTC](https://discourse.julialang.org/t/array-of-pointers-to-struct-vs-array-of-struct/83150 "2022-06-21T22:27:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.wolf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter.wolf/32/6018_2.png) [@peter.wolf](https://discourse.julialang.org/u/peter.wolf)\
**Post date:** [June 21, 2022, 10:27pm UTC](https://discourse.julialang.org/t/array-of-pointers-to-struct-vs-array-of-struct/83150/1 "2022-06-21T22:27:15Z")

</div>

I want to create a vector of structs. In my code I do something like this

```julia
some_vector = Vector{SomeStruct}(big_number)
for i in 1:big_number
    some_vector[i] = SomeStruct(some_args)
end

```

I believe this is allocating a small amount of memory many times. Once for each struct, and once for the vector. The vector just contains pointers to the structs.

Can I allocate an array of structs which is just one huge block of memory? If so, would that actually be more efficient?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [June 21, 2022, 10:37pm UTC](https://discourse.julialang.org/t/array-of-pointers-to-struct-vs-array-of-struct/83150/2 "2022-06-21T22:37:13Z")

</div>

Are your structs mutable? If possible make them immutable so they can be stored inline, depending on your access pattern [GitHub - JuliaArrays/StructArrays.jl: Efficient implementation of struct arrays in Julia](https://github.com/JuliaArrays/StructArrays.jl) can be more efficient than the inline structs

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 21, 2022, 11:29pm UTC](https://discourse.julialang.org/t/array-of-pointers-to-struct-vs-array-of-struct/83150/3 "2022-06-21T23:29:54Z")

</div>

Does your code compiles? I think it should be:

```julia
some_vector = Vector{SomeStruct}(undef, big_number)

```

in the first line. There is no constructor for `Vector` that only takes a number.
