# When using Vector{T}(undef,n) constructor, what are the values set to?

**URL:** <https://discourse.julialang.org/t/when-using-vector-t-undef-n-constructor-what-are-the-values-set-to/75251>\
**Category:** General Usage\
**Created:** [January 26, 2022, 7:40pm UTC](https://discourse.julialang.org/t/when-using-vector-t-undef-n-constructor-what-are-the-values-set-to/75251 "2022-01-26T19:40:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [January 26, 2022, 7:40pm UTC](https://discourse.julialang.org/t/when-using-vector-t-undef-n-constructor-what-are-the-values-set-to/75251/1 "2022-01-26T19:40:29Z")

</div>

Hi!

If T = Float64, I observe that the values most often are set as some very small number, but is it possible for it to be a number such as “0.1”,“5” etc.?

The reason I ask is that I just found out that some code I had would produce unstable results if I used the undef initializer to pre-initialize the vector compared to using zeros(Float64,n).

By unstable I mean that the algorithm I am using would change results based on the initialized values of the undef vector.

Kind regards

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 26, 2022, 7:45pm UTC](https://discourse.julialang.org/t/when-using-vector-t-undef-n-constructor-what-are-the-values-set-to/75251/2 "2022-01-26T19:45:07Z")

</div>

The values you get are whatever is in memory. They are actually not set to anything at all, so the values are completely _undefined_.

If you want your data initialized to `0`, then use `zeros`. For other values, use `fill!`.

See also [Faster zeros with calloc](https://discourse.julialang.org/t/faster-zeros-with-calloc/69860)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [January 26, 2022, 7:46pm UTC](https://discourse.julialang.org/t/when-using-vector-t-undef-n-constructor-what-are-the-values-set-to/75251/3 "2022-01-26T19:46:38Z")

</div>

Ah okay, thanks for the clarification. So it just takes any suitable amount of space available, without consideration of the previous data there, got it.

Kind regards

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 26, 2022, 7:48pm UTC](https://discourse.julialang.org/t/when-using-vector-t-undef-n-constructor-what-are-the-values-set-to/75251/4 "2022-01-26T19:48:33Z")

</div>

`zeros` is basically just

```julia
a = Vector{T}(undef, n)
fill!(a, zero(T))

```

Using `undef` does not clear the space for you. You use `undef` when you are going to manually initialize the values anyways, so there is no point in trying to fill it with `zeros`.
