# How is copy! legal when the size of src and dst array are different?

**URL:** <https://discourse.julialang.org/t/how-is-copy-legal-when-the-size-of-src-and-dst-array-are-different/113151>\
**Category:** Internals & Design\
**Created:** [April 18, 2024, 3:11pm UTC](https://discourse.julialang.org/t/how-is-copy-legal-when-the-size-of-src-and-dst-array-are-different/113151 "2024-04-18T15:11:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhinav\_Bhatia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhinav_bhatia/32/18748_2.png) [@Abhinav\_Bhatia](https://discourse.julialang.org/u/Abhinav_Bhatia)\
**Post date:** [April 18, 2024, 3:11pm UTC](https://discourse.julialang.org/t/how-is-copy-legal-when-the-size-of-src-and-dst-array-are-different/113151/1 "2024-04-18T15:11:33Z")

</div>

How is something like this legal?

x = rand(2)  
y = rand(3)  
copy!(x, y) # in-place copying should be impossible.

This is somehow legal and leads to creation of a new array for x and y is then copied to x.  
Shouldn’t this throw an error instead?

I am on Julia v1.9.4

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [April 18, 2024, 3:20pm UTC](https://discourse.julialang.org/t/how-is-copy-legal-when-the-size-of-src-and-dst-array-are-different/113151/2 "2024-04-18T15:20:59Z")

</div>

you can use the macro `@less` to get an idea of what happened:

```julia
@less copy!(x, y)

function copy!(dst::AbstractVector, src::AbstractVector)
    firstindex(dst) == firstindex(src) || throw(ArgumentError(
        "vectors must have the same offset for copy! (consider using `copyto!`)"))
    if length(dst) != length(src)
        resize!(dst, length(src))
    end
    copyto!(dst, src)
end

```

so you can see `x` will be resized in-place

---

<div class="post-metadata">

**Author:** ![Abhinav\_Bhatia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhinav_bhatia/32/18748_2.png) [@Abhinav\_Bhatia](https://discourse.julialang.org/u/Abhinav_Bhatia)\
**Post date:** [April 18, 2024, 4:18pm UTC](https://discourse.julialang.org/t/how-is-copy-legal-when-the-size-of-src-and-dst-array-are-different/113151/3 "2024-04-18T16:18:26Z")

</div>

Oh boy. I didn’t expect this. There might be some silent bugs in my codebase 🙁. Thanks.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 18, 2024, 5:06pm UTC](https://discourse.julialang.org/t/how-is-copy-legal-when-the-size-of-src-and-dst-array-are-different/113151/4 "2024-04-18T17:06:04Z")

</div>

[`copyto!`](https://docs.julialang.org/en/v1/base/arrays/#Base.copyto!-Tuple%7BAbstractArray,%20CartesianIndices,%20AbstractArray,%20CartesianIndices%7D) is more conservative here:

```julia
julia> copyto!(x, y)
ERROR: BoundsError: attempt to access 2-element Vector{Float64} at index [1:3]

```

and so is `.=`:

```julia
julia> x .= y
ERROR: DimensionMismatch: array could not be broadcast to match destination

```

In consequence, I rarely use `copy!` for arrays.
