tal*_*ees 7 arrays pointers reference nim-lang
var b: array[5, int]
type
ArrRef = ref array[5, int]
var c : ArrRef
echo repr(c) # nil
c = addr b # doesn't compile, says type is Array constructor, expected reference
Run Code Online (Sandbox Code Playgroud)
在Nim中,如何将引用传递给数组而不是传递值?到目前为止,请参阅上面的代码.
在Nim中,refs是在堆上,必须分配new.你不能只使用堆栈数组作为ref因为这是不安全的:当数组从堆栈中消失时,ref指向一些错误的内存.相反,你有两个选择:你可以使用不安全的ptrs.除了refs之外,它们不是垃圾收集的,可以用于不安全的东西.或者你可以做b一个ref array直接.