`nunsafe_convert`对于``vector {u}`对于索引而不是uinerange {u}

发布于 2025-02-13 22:51:14 字数 555 浏览 1 评论 0原文

我注意到用索引向量而不是unitrange给出的索引处理索引时,我注意到了以下错误。

c = rand(10)
c1 = view(c, [1, 3])
c2 = view(c1, 2:2)
Base.unsafe_convert(Ptr{Float64}, c2)

现在返回

ERROR: conversion to pointer not defined for SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] unsafe_convert(#unused#::Type{Ptr{Float64}}, a::SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false})
   @ Base ./pointer.jl:67
 [3] top-level scope
   @ REPL[6]:1

I noticed the following error when handling views with indices given by a vector of indices instead of a UnitRange.

c = rand(10)
c1 = view(c, [1, 3])
c2 = view(c1, 2:2)
Base.unsafe_convert(Ptr{Float64}, c2)

now returns

ERROR: conversion to pointer not defined for SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] unsafe_convert(#unused#::Type{Ptr{Float64}}, a::SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false})
   @ Base ./pointer.jl:67
 [3] top-level scope
   @ REPL[6]:1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2025-02-20 22:51:14

它们都会给我带来相同的错误(即,对于不连续的视图,明确不允许使用指针 - 由false> false键入参数指示):

julia> Base.unsafe_convert(Ptr{Float64}, c1)
ERROR: conversion to pointer not defined for SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] unsafe_convert(#unused#::Type{Ptr{Float64}}, a::SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false})
   @ Base ./pointer.jl:67
 [3] top-level scope
   @ REPL[8]:1

julia> Base.unsafe_convert(Ptr{Float64}, c2)
ERROR: conversion to pointer not defined for SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] unsafe_convert(#unused#::Type{Ptr{Float64}}, a::SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false})
   @ Base ./pointer.jl:67
 [3] top-level scope
   @ REPL[9]:1

这是有道理的 - 连续块并不是真正有意义的(有关选定索引的信息丢失了)。

但是,您可以为一个范围获得一个指针:

julia> Base.unsafe_convert(Ptr{Float64}, view(c, 2:2))
Ptr{Float64} @0x00007f1feaa58b88

They both result in the same error for me (i.e., taking pointers is just explicitely disallowed for non-contiguous views -- indicated by the false type parameter):

julia> Base.unsafe_convert(Ptr{Float64}, c1)
ERROR: conversion to pointer not defined for SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] unsafe_convert(#unused#::Type{Ptr{Float64}}, a::SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false})
   @ Base ./pointer.jl:67
 [3] top-level scope
   @ REPL[8]:1

julia> Base.unsafe_convert(Ptr{Float64}, c2)
ERROR: conversion to pointer not defined for SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] unsafe_convert(#unused#::Type{Ptr{Float64}}, a::SubArray{Float64, 1, Vector{Float64}, Tuple{Vector{Int64}}, false})
   @ Base ./pointer.jl:67
 [3] top-level scope
   @ REPL[9]:1

Which makes sense, as a pointer to a non-contiguous chunk is not really meaningful (the information about the selected indices is lost).

You can get a pointer for a range, though:

julia> Base.unsafe_convert(Ptr{Float64}, view(c, 2:2))
Ptr{Float64} @0x00007f1feaa58b88
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文