# Why do unsafe operations check bounds for \`Memory\`?

**URL:** <https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152>\
**Category:** Internals & Design\
**Created:** [November 24, 2026, 1:14pm UTC](https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152 "2026-11-24T13:14:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [November 24, 2026, 1:14pm UTC](https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152/1 "2026-11-24T13:14:24Z")

</div>

I’ve noticed that some “unsafe” operations for `Memory` actually do check bounds:

```julia-auto
julia> v = Memory{Int}(1:3)
3-element Memory{Int64}:
 1
 2
 3

julia> unsafe_copyto!(v, 1, v, 100, 3)
ERROR: BoundsError: attempt to access 3-element Memory{Int64} at index [100]
[...]

julia> g(v, i) = @inbounds v[i]
g (generic function with 1 method)

julia> g(v, 1:100)
ERROR: BoundsError: attempt to access 3-element Memory{Int64} at index [1:100]
[...]

```

Is this intended? If not (and if I find the time), I might prepare a PR to fix this.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 24, 2026, 5:12pm UTC](https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152/2 "2026-11-24T17:12:52Z")

</div>

edit: Oops, I failed at reading comprehension for the issue.  
~~  
On what version do you observe this? I don’t see that, a la~~

~~

```julia-auto
julia> m=[1].ref.mem
1-element Memory{Int64}:
 1

julia> g(v,i)=@inbounds v[i]
g (generic function with 1 method)

julia> g(m,2)
140352011457840

```

~~

tested on both 1.13.4 and 1.15.0-DEV.971 with `x86_64-linux-gnu` and default `--check-bounds=auto`.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [November 24, 2026, 5:41pm UTC](https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152/3 "2026-11-24T17:41:15Z")

</div>

I observe it on all version from 1.11 to nightly. Note that in my first post I called `g` with a range as second argument. With an integer there is indeed no bounds check.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [November 24, 2026, 6:13pm UTC](https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152/4 "2026-11-24T18:13:33Z")

</div>

It seems getindex isn’t forwarding the inbounds to copyto!, which runs bounds checks again.

---

<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:** [November 24, 2026, 7:31pm UTC](https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152/5 "2026-11-24T19:31:17Z")

</div>

see: [inbounds propagation mostly missing in base/genericmemory.jl? · Issue #56145 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/56145) [propagate inbounds in a few more places for memoryref - Pull Request #56151 - JuliaLang/julia - GitHub](https://github.com/JuliaLang/julia/pull/56151) and probably some other refs

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [November 24, 2026, 7:50pm UTC](https://discourse.julialang.org/t/why-do-unsafe-operations-check-bounds-for-memory/143152/6 "2026-11-24T19:50:51Z")

</div>

We kept the checks cause they are functionally free in these operations (they usually lower to memcpy/memset) so there’s not much reason to take them out.  
The overhead of a bounds check is really only visible if it affects automatic vectorization
