Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example use of @inbounds in the @inbounds documentation uses it incorrectly #39367

Closed
yurivish opened this issue Jan 23, 2021 · 0 comments
Closed
Labels
docs This change adds or pertains to documentation

Comments

@yurivish
Copy link
Contributor

It appears that the example use of @inbounds given in the documentation for @inbounds is not a safe use of @inbounds:

image

The paragraph above the example implies that it is meant as an example to be followed, not one to be avoided. However, the warning immediately below the example strongly recommends only using @inbounds when it is certain from the information locally available that all accesses are in bounds.

This is not the case in the example, whose method signature explicitly allows all AbstractArrays, including those which do not use one-based indexing.

I verified that the example silently produces incorrect results due to out-of-bounds memory access on Julia 1.5.3:

julia> function sum(A::AbstractArray)
           r = zero(eltype(A))
           for i = 1:length(A)
               @inbounds r += A[i]
           end
           return r
       end
sum (generic function with 1 method)

julia> using OffsetArrays

julia> sum(OffsetArray(collect(1:11), -5:5))
8858289599
@yurivish yurivish changed the title the example use of @inbounds in the documentation for @inbounds not a correct use of @inbounds example use of @inbounds in the @inbounds documentation uses it incorrectly Jan 23, 2021
@JeffBezanson JeffBezanson added the docs This change adds or pertains to documentation label Jan 25, 2021
KristofferC pushed a commit that referenced this issue Jun 3, 2022
KristofferC pushed a commit that referenced this issue Jul 4, 2022
KristofferC pushed a commit that referenced this issue Dec 21, 2022
staticfloat pushed a commit that referenced this issue Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

2 participants