A MemoryPointer is a specific {Pointer}. It points to a memory composed of cells. All cells have the same size.
@example Create a new MemoryPointer
mp = FFI::MemoryPointer.new(:long, 16) # Create a pointer on a memory of 16 long ints.
@example Create a new MemoryPointer from a String
mp1 = FFI::MemoryPointer.from_string("this is a string") # same as: mp2 = FFI::MemoryPointer.new(:char,16) mp2.put_string("this is a string")
static VALUE memptr_s_from_string(VALUE klass, VALUE to_str) { VALUE s = StringValue(to_str); VALUE args[] = { INT2FIX(1), LONG2NUM(RSTRING_LEN(s) + 1), Qfalse }; VALUE obj = rb_class_new_instance(3, args, klass); rb_funcall(obj, rb_intern("put_string"), 2, INT2FIX(0), s); return obj; }
static VALUE memptr_initialize(int argc, VALUE* argv, VALUE self) { VALUE size = Qnil, count = Qnil, clear = Qnil; int nargs = rb_scan_args(argc, argv, "12", &size, &count, &clear); memptr_malloc(self, rbffi_type_size(size), nargs > 1 ? NUM2LONG(count) : 1, RTEST(clear) || clear == Qnil); if (rb_block_given_p()) { return rb_ensure(rb_yield, self, memptr_free, self); } return self; }