Subject:
[ruby-ffi] Why the address change?
From:
Simon Chiang
Date:
12/10/09 12:16 AM
To:
ruby-ffi

I've noticed the address of pointers can change when passed back into
an attached function multiple times.  How can I get the address to
stay the same?

For example, say I have these functions:

  #include <stdio.h>

  void *get_pointer()
  {
    void *ptr;
    return(ptr);
  }

  int print_pointer( void *ptr )
  {
    printf("pointer: %p\naddress: %p\n", ptr, &ptr);
    return(0);
  }

And then I do this:

  module ExampleCode
    attach_function :get_pointer, [], :pointer
    attach_function :print_pointer, [:pointer], :int
  end

  ptr = ExampleCode.get_pointer()

  puts
  puts "First Time:"
  ExampleCode.print_pointer(ptr)

  puts
  puts "Second Time:"
  ExampleCode.print_pointer(ptr)

I get this for an output:

  First Time:
  pointer: 0x6249
  address: 0x7fff5fbfef38

  Second Time:
  pointer: 0x6249
  address: 0x7fff5fbfeef8

I'm on OS X 10.6.2, using FFI 0.5.4.  Thanks!