# BoundsError in CartesianIndices on 32-bit Windows

**URL:** <https://discourse.julialang.org/t/boundserror-in-cartesianindices-on-32-bit-windows/112777>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 10, 2024, 2:31pm UTC](https://discourse.julialang.org/t/boundserror-in-cartesianindices-on-32-bit-windows/112777 "2024-04-10T14:31:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Expine](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@Expine](https://discourse.julialang.org/u/Expine)\
**Post date:** [April 10, 2024, 2:31pm UTC](https://discourse.julialang.org/t/boundserror-in-cartesianindices-on-32-bit-windows/112777/1 "2024-04-10T14:31:22Z")

</div>

Hi!

I experienced Integer overflow on Windows due to the fact that Int32 is the default one there:

```julia
length(CartesianIndices((1000000, 1000000)))

```

Succeeds on linux64 but fails on Windows (something like A[-72232] is out of bounds).

I copied `randsubseq!` implementation, where the overflow happens, and adjusted it so index vars are Int64 now:

```julia
randsubseq(r::AbstractRNG, A::AbstractArray{T}, p::Real) where {T} =
    randsubseq!(r, T[], A, p)

# Fill S (resized as needed) with a random subsequence of A, where
# each element of A is included in S with independent probability p.
function randsubseq!(r::AbstractRNG, S::AbstractArray, A::AbstractArray, p::Real)
    Base.require_one_based_indexing(S, A)
    0 <= p <= 1 || throw(ArgumentError("probability $p not in [0,1]"))
    n = prod(size(A), init=Int64(1)) # length - there the overflow happened originally
    p == 1 && return copyto!(resize!(S, n), A)
    empty!(S)
    p == 0 && return S
    nexpected = p * n
    sizehint!(S, round(Int64, nexpected + 5*sqrt(nexpected)))
    if p > 0.15
        for i = 1:n
            rand(r) <= p && push!(S, A[i])
        end
    else
        L = -1 / log1p(-p)
        i = Int64(0)
        while true
            s = randexp(r) * L
            s >= n - i && return S
            push!(S, A[i += ceil(Int64, s)])
        end
    end
    return S
end

```

Surprisingly I get:

```julia
  BoundsError: attempt to access 1000000×1000000 CartesianIndices{2, Tuple{Base.OneTo{Int32}, Base.OneTo{Int32}}} at index [10504]

```

on Windows, which doesn’t really make sense. How come I can’t access something that is in bounds?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 10, 2024, 2:48pm UTC](https://discourse.julialang.org/t/boundserror-in-cartesianindices-on-32-bit-windows/112777/2 "2024-04-10T14:48:42Z")

</div>

Welcome! So, the reason that CartesianIndices exists is to be used as an index — and on a 32-bit system, you cannot create a thing that has more than the ~2 billion addressable elements. A 1000000x1000000 array has a trillion elements.

What are you trying to do here? I suspect there’s a much better solution than a trillion iterations in the first place.

---

<div class="post-metadata">

**Author:** ![Expine](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@Expine](https://discourse.julialang.org/u/Expine)\
**Post date:** [April 10, 2024, 3:15pm UTC](https://discourse.julialang.org/t/boundserror-in-cartesianindices-on-32-bit-windows/112777/3 "2024-04-10T15:15:09Z")

</div>

So I work with a sparse tensor library `Finch.jl`.

There’s a method there `fsprand`, fiber-sparse-rand that returns a COO tensor, given dimensions and density `p`:

> **[Documentation Listing · Finch.jl](https://willowahrens.io/Finch.jl/stable/reference/listing/#Finch.fsprand-Tuple-reference-listing)**
>
> Documentation for Finch.jl.

Internally:

```julia
fsprand(1_000_000, 1_000_000, 1e-06)

```

uses `CartesianIndices` to describe only non-zeros, and calls:

```julia
randsubseq(r, CartesianIndices(M), p)

```

where `M=(1_000_000, 1_000_000)` and `p=1e-06`, which fails on Windows.

We have an open PR with some discussion: [Wma/fix 488 by willow-ahrens · Pull Request #489 · willow-ahrens/Finch.jl · GitHub](https://github.com/willow-ahrens/Finch.jl/pull/489)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 10, 2024, 3:31pm UTC](https://discourse.julialang.org/t/boundserror-in-cartesianindices-on-32-bit-windows/112777/4 "2024-04-10T15:31:00Z")

</div>

Ah, of course, the non-sensical bounds error is because, when you use linear indexing, it’ll check the index against the length. And the length overflows to negative, so _everything_ is going to be OOB.

The crux of it is that arrays (even sparse ones) with more than 2^31 elements are going to be troublesome on 32-bit systems.
