使用 ruby FFI 在 ruby 中创建静态数组 class

Creating a static array class in ruby with ruby FFI

我想在 ruby 中实现我自己的静态数组 class。它将是一个具有固定容量的数组,并且数组中的所有元素都是单一类型。 为了直接访问内存,我使用了 FFI gem https://github.com/ffi/ffi,它可以创建您自己的 C 函数并在您的 ruby 程序中使用它们。 我创建了一个非常简单的 C 函数,它为整数数组和 returns 指向内存的指针分配内存 space :

int * create_static_array(int size) {
  int *arr = malloc(size * sizeof(int));
  return arr;
}

这是我的 ruby static_array class 使用 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 取代...我真的不明白这是怎么回事?

如果有帮助,这里是 FFIPointer class 文档的 link https://www.rubydoc.info/github/ffi/ffi/FFI/Pointer

FFI 接口不知道您的指针类型,因此它只是将其视为字节数组(请参阅指针类型初始化)。请注意,虽然您确实传递了 :int,但这是针对特定的 writeread,而不是您进行索引的地方。因此,您在字节偏移量 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 000x00001014 或 4116,然后在下一个偏移处打印它是 10 00 00 00,即 16.