Subject: [ruby-ffi] Re: What is the best way to convert a Fixnum to a :pointer? |
From: Wayne Meissner |
Date: 9/15/09 9:15 PM |
To: ruby-ffi@googlegroups.com |
A MemoryPointer is an allocated Pointer. i.e. it handles allocation and deallocation of the memory. FFI::MemoryPointer.new(x) tries to allocate x bytes. FFI::Pointer.new on the other hand, just wraps whatever you give it in a Pointer instance btw, you should be checking for Integer in your code, not Fixnum. A Fixnum in MRI cannot hold all the bits in a pointer. e.g. on 32bit: a = 0xfee1dead # this is a valid 32bit address puts "a.is_a?(Fixnum)=#{a.is_a?(Fixnum)}" # => false puts "a.is_a?(Integer)=#{a.is_a?(Integer)}" # => true Or the better way is to do: pointer = address.is_a?(FFI::Pointer) ? address : FFI::Pointer.new(address) Or you could always just wrap it using FFI::Pointer.new(address), since FFI::Pointer.new can take pointer arguments as well as Integer arguments 2009/9/16 Brett Blackham <brett.blackham@gmail.com>:
Ah ha! Thanks. pointer = FFI::Pointer.new(address) # Works pointer = FFI::MemoryPointer.new(address) # Fails. Thanks again! Now I am curious what is the difference between a Pointer and MemoryPointer. It was my understanding that a pointer is a pointer.