# Pre-Allocate Array Custom Struct with Zeroed Values

**URL:** <https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823>\
**Category:** General Usage\
**Created:** [October 6, 2020, 1:55am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823 "2020-10-06T01:55:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RodBiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodbiren/32/16979_2.png) [@RodBiren](https://discourse.julialang.org/u/RodBiren)\
**Post date:** [October 6, 2020, 1:55am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823/1 "2020-10-06T01:55:35Z")

</div>

I am trying to figure out how to pre-allocate an array with a custom struct with zeroed values. I figured out how to allocate using undef, but I am uncertain how I can do that while setting the values other than a loop. It feels like I am missing something.

Method that works

```julia
using DataFrames, Dates

struct MyData
    rev::Float64
    qty::Int64
    MyData() = new(0,0)
end

arr =[]
for i in 1:10
    push!(arr,MyData())
end

time_series = DataFrame()
for date in Date(2019,1):Dates.Month(1):Date(2020,12)
    time_series[Symbol(date)] = arr
end

ts
10×24 DataFrame. Omitted printing of 17 columns
│ Row │ 2019-01-01 │ 2019-02-01 │ 2019-03-01 │ 2019-04-01 │ 2019-05-01 │ 2019-06-01 │ 2019-07-01 │
│ │ Any │ Any │ Any │ Any │ Any │ Any │ Any │     
├───────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┤     
│ 1 │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │     
│ 2 │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │     
│ 3 │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │
│ 4 │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │     
│ 5 │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │     
│ 6 │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │     
│ 7 │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │ MyData(0.0, 0) │     

```

Thing I sort of wish I could do

```julia
for date in Date(2019,1):Dates.Month(1):Date(2020,12)
    time_series[Symbol(date)] = Array{MyData}(ZeroSomehow,100)
end

```

Am I missing a more Julian way of getting this done? I checked the source for answers, but I was unable to figure out much from UndefInitializer. I am not good at digging through it yet.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 6, 2020, 2:05am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823/2 "2020-10-06T02:05:18Z")

</div>

> [@RodBiren](#):
>
> ```julia
> arr =[]
> for i in 1:10
> push!(arr,MyData())
> end
> 
> ```

You mean this part?

Coud be done with:

```julia
arr = [MyData() for i in 1:10]

```

for example.

Edit: Yet, you would not be able to change the values, because they are immutable. (probably this is what you are asking?)

The options are: 1) use a mutabe struct instead or 2) whenever you want to change the content of `arr`, you have to initialize the complete struct in each position. I would do:

```julia
struct MyData
  rev :: Float64
  qty :: Int64
end
MyData() = MyData(0,0)
arr = [MyData() for i in 1:10]
arr[5] = MyData(1,1) # for example

```

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [October 6, 2020, 2:06am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823/3 "2020-10-06T02:06:13Z")

</div>

I’m not sure what you are trying to do, but

```julia
Vector{MyData}(undef, 10)

```

should work to preallocate with `undef`. If you need zeros, then you have to fill it, e.g., with

```julia
fill(MyData(), 10)

```

---

<div class="post-metadata">

**Author:** ![RodBiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodbiren/32/16979_2.png) [@RodBiren](https://discourse.julialang.org/u/RodBiren)\
**Post date:** [October 6, 2020, 2:13am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823/4 "2020-10-06T02:13:23Z")

</div>

Yup, I was going for mutable. I am trying to vcat some DataFrames together, so I know the size of the dataframe before adding data to it. Not sure if it makes sense to try and allocated it before hand or not.

---

<div class="post-metadata">

**Author:** ![RodBiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodbiren/32/16979_2.png) [@RodBiren](https://discourse.julialang.org/u/RodBiren)\
**Post date:** [October 6, 2020, 2:16am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823/5 "2020-10-06T02:16:00Z")

</div>

I think I was looking for fill and somehow kept missing how to use it. I think that is what I am looking for. Now that i am looking at it I feel that I am focusing too much on allocating memory early. I am just not sure how to find a middle ground between undef and a union with nothing.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 6, 2020, 2:17am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823/6 "2020-10-06T02:17:47Z")

</div>

I think it is generally better to keep it immutable and redefine the structures in the desired positions (the vector as whole is mutable).

---

<div class="post-metadata">

**Author:** ![RodBiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodbiren/32/16979_2.png) [@RodBiren](https://discourse.julialang.org/u/RodBiren)\
**Post date:** [October 6, 2020, 2:25am UTC](https://discourse.julialang.org/t/pre-allocate-array-custom-struct-with-zeroed-values/47823/7 "2020-10-06T02:25:38Z")

</div>

I ended up just making some arrays to hold the values then cast them into a DataFrame once I was done parsing. I keep getting caught up in types and allocating. Probably my c++ background showing. Just seemed like a thing I should have been able to do. turns out I made some sort of recursive pointer doing it the way I showed and everything was just a reference to arr. Cost of abstraction I suppose.

Thank you all so much
