3

My code in C++ is

StructureEx* obj; // structure
functionEx((void**)&obj);

and my function is

int functionEx(void** obj); //calling function

I am new to CFFI. So my question is

  1. How can I achieve the same in CFFI?

  2. How to find the address of a pointer, pointer to structure in CFFI?

I know casting to void** can be done by

ffi.cast("void*",address)

But how can I get that address and pass to the function?

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
Ash
  • 809
  • 1
  • 8
  • 14

1 Answers1

3

It is possible to declare arg = ffi.new("void **") that may be usable.

The following code prints

<cdata 'void *' NULL>

<cdata 'void *' 0xc173c0>

7

i.e. first the value of pointer is zero, and after the call the value corresponds to the value set in functionEx.

from cffi import FFI

ffi = FFI()
ffi.cdef("""int functionEx(void** obj);""")

C = ffi.dlopen("./foo.so")
print(C)
arg = ffi.new("void **")
print(arg[0])
C.functionEx(arg)
print(arg[0])
ints = ffi.cast("int *", arg[0])
print(ints[7])
#include <stdio.h>
#include <stdlib.h>

int functionEx(void ** obj)
{
    int * arr;
    int i;

    *obj = malloc(sizeof(int) * 8);

    arr = *obj;
    for (i=0; i<8; i++) {
        arr[i] = i;
    }

    return 0;
}
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • Thank you Hakala for such a clear explanation. Can you please explain me whether the particular case stated below works 1) I have allocated memory of a structure for a variable struct_object it shows me 2) created a void** variable void_object=cfg_cffi.ffi.new("void**") 3) Assign the memory of struct variable to void** void_object[0]=struct_object 4) Now pass void_object[0] for the function stated in the question The program works without error but returns -1 which is particular failed condition according to the implementation. – Ash Jul 25 '16 at 04:31
  • Have you tried passing `void_object` instead of `void_object[0]`? – J.J. Hakala Jul 25 '16 at 04:41