我想在 ruby 中实现我自己的静态数组类。它将是一个具有固定容量的数组,并且数组中的所有元素都将是单一类型。 为了直接访问内存,我使用 FFI gem https://github.com/ffi/ffi它允许创建您自己的 C 函数并在您的 ruby 程序中使用它们。 我创建了一个非常简单的 C 函数,它为整数数组分配内存并返回指向内存空间的指针:
int * create_static_array(int size) {
int *arr = malloc(size * sizeof(int));
return arr;
}
这是我的 ruby static_array 类,它使用 create_static_array :
require 'ffi'
class StaticArray
attr_accessor :pointer, :capacity, :next_index
extend FFI::Library
ffi_lib './create_array/create_array.so'
attach_function :create_static_array, [:int], :pointer
def initialize(capacity)
@capacity = capacity
@pointer = create_static_array(capacity)
@next_index = 0
end
# adds value to the next_index in array
def push(val)
@pointer[@next_index].write(:int, val)
@next_index += 1
end
# reads value at index
def [](index)
raise IndexOutOfBoundException if index >= @capacity
self.pointer[index].read(:int)
end
# print every value in index
def print
i = 0
while (i < @capacity)
puts @pointer[i].read(:int)
i += 1
end
end
end
我添加了一些方法来与我的数组交互,推送元素,读取索引处的元素...... 但是我的 static_array 实例并没有完全按预期工作...
假设我写的是:
// creates a static array in memory which can store 4 ints
arr = StaticArray.new(4)
现在让我们在 arr 中压入一个 int :
arr.push(20)
arr.print
将输出
20
0
0
0
这是有道理的。现在让我们将另一个 int 插入 arr 中:
arr.push(16)
再次arr.print
:
4116
16
0
0
20 已被 4116 取代...我真的不明白这里发生了什么?
最佳答案
FFI 接口(interface)不知道指针的类型,因此它只是将其视为字节数组(请参阅指针类型的初始化)。请注意,虽然您确实传递了 :int
,但这是针对特定的 write
和 read
,而不是您执行索引的位置。因此,您正在字节偏移量 0,1,2,3 处写入和打印,而不是在 0,4,8,12 处的整数元素。
在小端系统上,对于 32 位、4 字节 int,二进制值 20 是 14 00 00 00
,16 是 10 00 00 00
。
所以你分配了 4*4 字节,即 32 个字节,前 8 个字节。
00 00 00 00 00 00 00 00
并在偏移量0处写入20
14 00 00 00 00 00 00 00
然后在偏移量1处写入16
14 10 00 00 00 00 00 00
14 10 00 00
是 0x00001014
或 4116,然后在下一个打印偏移量处是 10 00 00 00
,即 16 .
关于使用 ruby FFI 在 ruby 中创建静态数组类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571127/