Array of pointers to struct vs. Array of struct

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

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

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?

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 can be more efficient than the inline structs


Does your code compiles? I think it should be:

some_vector = Vector{SomeStruct}(undef, big_number)

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

1 Like