我正在尝试将一些代码从Python转换为Julia。python代码使用struct函数和memoryview模块。在Julia中有没有可以使用的等价物?
发布于 2021-07-29 13:37:18
虽然我不熟悉这些python模块,但如果我快速搜索它们的目的是正确的,那么它们在Julia中是不适用/不必要的,因为Julia structs在默认情况下已经可以与C进行互操作(例如:[1],[2]),并且Julia对象,如Strings,Arrays (包括行为良好的structs的Arrays )等,已经仅仅是内存加上一些元数据的视图。例如:
julia> A = fill(NaN,5)
5-element Vector{Float64}:
NaN
NaN
NaN
NaN
NaN
julia> reinterpret(Int64,A) # With no overhead, interpret the same block of memory as a different type
5-element reinterpret(Int64, ::Vector{Float64}):
9221120237041090560
9221120237041090560
9221120237041090560
9221120237041090560
9221120237041090560
julia> Ref(A) # Julia-style pointer
Base.RefValue{Vector{Float64}}([NaN, NaN, NaN, NaN, NaN])
julia> Base.pointer(A) # Native pointer
Ptr{Float64} @0x000000011e52c7d0https://stackoverflow.com/questions/68565579
复制相似问题