From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8275 invoked by alias); 5 Mar 2013 12:44:13 -0000 Received: (qmail 8247 invoked by uid 22791); 5 Mar 2013 12:44:12 -0000 X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,KHOP_RCVD_TRUST,KHOP_THREADED,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE,TW_BF X-Spam-Check-By: sourceware.org Received: from mail-bk0-f47.google.com (HELO mail-bk0-f47.google.com) (209.85.214.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 05 Mar 2013 12:44:02 +0000 Received: by mail-bk0-f47.google.com with SMTP id jc3so55120bkc.20 for ; Tue, 05 Mar 2013 04:44:01 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.204.136.197 with SMTP id s5mr9236301bkt.125.1362487441408; Tue, 05 Mar 2013 04:44:01 -0800 (PST) Received: by 10.204.42.73 with HTTP; Tue, 5 Mar 2013 04:44:01 -0800 (PST) In-Reply-To: <16f609a4-607d-4398-9ae5-fa9921795726@DB3EHSMHS013.ehs.local> References: <16f609a4-607d-4398-9ae5-fa9921795726@DB3EHSMHS013.ehs.local> Date: Tue, 05 Mar 2013 12:44:00 -0000 Message-ID: Subject: Re: Function pointers? From: The Devils Jester To: Nathan Rossi Cc: "libffi-discuss@sourceware.org" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact libffi-discuss-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libffi-discuss-owner@sourceware.org X-SW-Source: 2013/txt/msg00106.txt.bz2 I am not sure how that makes a difference. With the above change, the value stored in values[0] and the actual ptr printed in test_b are still different. Am I missing something obvious here? On Tue, Mar 5, 2013 at 1:46 AM, Nathan Rossi wrot= e: >> -----Original Message----- >> From: libffi-discuss-owner@sourceware.org [mailto:libffi-discuss- >> owner@sourceware.org] On Behalf Of The Devils Jester >> Sent: Tuesday, March 05, 2013 4:06 PM >> To: libffi-discuss@sourceware.org >> Subject: Function pointers? >> >> I can pass pointers as arguments to a function without issue unless it >> is a function pointer. When I try and pass a function pointer, it >> gets jumbled during the pass. What am I doing wrong? >> >> Here is an example code that prints the value of the pointer before >> its passed, and in the function its passed to. Both values are >> different, does anyone know why? >> >> (I am running OS X 10.8.2) >> >> #include >> #include >> >> int test_a(void*ptr) >> { return 0; } >> >> int test_b(void*ptr) >> { >> printf("Address of passed ptr: %p\n", ptr ); >> return 0; >> } >> >> int main() >> { >> ffi_cif cif; >> ffi_type *args[1]; >> void *values[1]; >> int rc; >> >> args[0] =3D &ffi_type_pointer; >> values[0] =3D &test_a; >> >> if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_uint, args) =3D= =3D FFI_OK) >> { >> printf("Address of ptr to pass: %p\n", values[0] ); >> ffi_call(&cif, test_b, &rc, values); >> } >> return 0; >> } > > Hi, > > The 'values' array you are passing to ffi_call needs to contain pointers = to the associated data (in this case the data is the function pointer). Bec= ause of the C syntax "test_a =3D=3D &test_a", and the & operator is ignored= , and thus you are passing the address of the function as the pointer to th= e data. > > You will need to store the function pointer on the stack/heap before pass= ing it to the ffi_call. E.g. > .. > void* arg_values[1]; > arg_values[0] =3D (void*)test_a; > values[0] =3D &(arg_values[0]); > ... > > Regards, > Nathan > >