使用ctypes将malloc'ed数组从C返回到Python

时间:2014-08-20 15:45:14

标签: python c arrays ctypes ctype

我希望使用一些返回多个未知大小数组的C代码。因为有多个数组,我想我需要使用传入的指针,我不知道如何将它与malloc相结合,malloc用于设置数组。

这是一些有代表性的C代码:

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

//gcc -fPIC -shared -o array_test_c.so array_test_c.c

void return_array(int * outdata1v, int * outdata2v) {
    int i;
    int N = 10;
    int * mydatav2, * mydatav3;
    mydatav2 = (int *) malloc(sizeof(int) * N);
    mydatav3 = (int *) malloc(sizeof(int) * N);
    for (i = 0; i<N; i++){
        mydatav2[i] = i;
        mydatav3[i] = i*2;
    }

    //this doesn't work which makes sense
    outdata1v = mydatav2;
    outdata2v = mydatav3;
}

我试图用这些东西将它挂钩到Python(这不起作用):

import os
import ctypes


#for c interface
test_module = ctypes.cdll.LoadLibrary(
    os.path.join(os.path.dirname(__file__), './array_test_c.so'))

outdata1 = (ctypes.c_int * 0)()
outdata2 = (ctypes.c_int * 0)()
test_module.return_array(outdata1, outdata2)
outdata1 = (ctypes.c_int*10).from_address(ctypes.addressof(outdata1))
print "out", outdata1[-1], outdata1, outdata2

这不起作用,我永远不会打印20。有什么想法吗?

2 个答案:

答案 0 :(得分:4)

<强> test.c的

#include <stdlib.h>

#define N 10

void test(int *size, int **out1, int **out2) {
    int i;
    int *data1, *data2;
    data1 = (int *)malloc(sizeof(int) * N);
    data2 = (int *)malloc(sizeof(int) * N);
    for (i = 0; i < N; i++){
        data1[i] = i;
        data2[i] = i * 2;
    }
    *size = N;
    *out1 = data1;
    *out2 = data2;
}

<强> test.py

from ctypes import CDLL, POINTER, c_int, byref

dll = CDLL('test.so')

data1 = POINTER(c_int)()
data2 = POINTER(c_int)()
size = c_int()

dll.test(byref(size), byref(data1), byref(data2))

for i in range(size.value):
    print i, data1[i], data2[i]

编辑:你应该考虑提供一个释放malloc数据的功能。所以你可以这样做,例如dll.cleanup(data1, data2)

答案 1 :(得分:2)

你需要指针指针:

void return_array(int **outdata1v, int **outdata2v) {
    int i;
    int N = 10;
    int * mydatav2, * mydatav3;
    mydatav2 = (int *) malloc(sizeof(int) * N);
    mydatav3 = (int *) malloc(sizeof(int) * N);
    for (i = 0; i<N; i++){
        mydatav2[i] = i;
        mydatav3[i] = i*2;
    }

    *outdata1v = mydatav2;
    *outdata2v = mydatav3;
}

我不了解python部分。