# Bug in julia zeros() side effects

**URL:** <https://discourse.julialang.org/t/bug-in-julia-zeros-side-effects/77046>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 25, 2022, 2:32am UTC](https://discourse.julialang.org/t/bug-in-julia-zeros-side-effects/77046 "2022-02-25T02:32:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![herm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herm/32/14268_2.png) [@herm](https://discourse.julialang.org/u/herm)\
**Post date:** [February 25, 2022, 2:32am UTC](https://discourse.julialang.org/t/bug-in-julia-zeros-side-effects/77046/1 "2022-02-25T02:32:13Z")

</div>

Julia Version 1.7.2 (2022-02-06)

I experienced some side effects of the initialization with zeros.  
Good to know…

# buggy

function compute\_k1!(nfft::Int64,kvec::Vector{ComplexF64})  
kvec=zeros(ComplexF64,nfft)  
for i=1:nfft  
kvec[i]=i+1im  
end  
end

# no bug

function compute\_k2!(nfft::Int64,kvec::Vector{ComplexF64})  
for i=1:nfft  
kvec[i]=i+1im  
end  
end

nfft=10  
kvec1=zeros(ComplexF64,nfft)  
compute\_k1!(nfft,kvec1)  
println(kvec1)  
kvec2=zeros(ComplexF64,nfft)  
compute\_k2!(nfft,kvec2)  
println(kvec2)

first println  
10-element Vector{ComplexF64}:  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
0.0 + 0.0im  
second println  
ComplexF64[1.0 + 1.0im, 2.0 + 1.0im, 3.0 + 1.0im, 4.0 + 1.0im, 5.0 + 1.0im, 6.0 + 1.0im, 7.0 + 1.0im, 8.0 + 1.0im, 9.0 + 1.0im, 10.0 + 1.0im]

Obviously there is side effects on using zeros…

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 25, 2022, 2:33am UTC](https://discourse.julialang.org/t/bug-in-julia-zeros-side-effects/77046/2 "2022-02-25T02:33:32Z")

</div>

This isn’t a bug. This is just that Julia is pass by sharing.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 25, 2022, 3:20am UTC](https://discourse.julialang.org/t/bug-in-julia-zeros-side-effects/77046/3 "2022-02-25T03:20:34Z")

</div>

In the function `compute_k1!` you create a local array which masks `kvec`  
passed as argument. The argument is not modified. Delete the line which  
calls `zeros`. If you really want to set `kvec` to zero on that line, use `kvec .= zero(eltype(kvec))`.

---

<div class="post-metadata">

**Author:** ![herm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herm/32/14268_2.png) [@herm](https://discourse.julialang.org/u/herm)\
**Post date:** [February 26, 2022, 9:30am UTC](https://discourse.julialang.org/t/bug-in-julia-zeros-side-effects/77046/4 "2022-02-26T09:30:09Z")

</div>

> [@PetrKryslUCSD](#):
>
> `kvec .= zero(eltype(kvec))` .

Thank you for the response, topics closed

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 26, 2022, 9:48am UTC](https://discourse.julialang.org/t/bug-in-julia-zeros-side-effects/77046/5 "2022-02-26T09:48:14Z")

</div>

When posting code, please make sure to wrap it in triple backticks so it gets formatted. You can read about it here:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …
