Subject: Re: [ruby-ffi] Why the address change? |
From: Wayne Meissner |
Date: 12/10/09 12:42 AM |
To: ruby-ffi@googlegroups.com |
In print_pointer(), the 'ptr' variable is on the stack, so the address of it can change with every call, as it can with any stack variable. The same thing can happen in C. e.g. if you combined your C code with the following test harness, it would also print out different values for &ptr. void indirect_print(void* ptr) { print_pointer(ptr); } int main(int argc, char **argv) { void* ptr = (void*) (intptr_t) 0x1234; print_pointer(ptr); indirect_print(ptr); } The value of the pointer, which is the thing you are interested in, does not change. Neither does it with FFI (as your output showed). 2009/12/10 Simon Chiang <simon.a.chiang@gmail.com>:
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!