如何找到指向结构的指针的地址并将其转换为 CFFI 中的 void**

How to find address of a pointer to structure and cast it to void** in CFFI

我的 C++ 代码是

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

我的职能是

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

我是 CFFI 的新手。所以我的问题是

  1. 如何在 CFFI 中实现同样的效果?

  2. 如何在CFFI中找到指针的地址,指向结构体的指针?

我知道转换为 void** 可以通过

完成
ffi.cast("void*",address)

但是我怎样才能得到那个地址并传递给函数呢?

可以声明arg = ffi.new("void **")可能可用。

以下代码打印

<cdata 'void *' NULL>

<cdata 'void *' 0xc173c0>

7

即首先指针的值为零,调用后对应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;
}