1

我正在尝试访问一个使用 ctypes 返回结构指针的 C 函数。我正在使用来自 EPD 的 32 位 ipython。我在 64 位 CentOS 机器上,但我必须使用 32 位 Python,因为共享库是 32 位的。当我尝试获取结构指针时,出现分段错误。我不需要访问结构字段,因为我只是将结构用作另一个 C 函数的输入。代码看起来像这样。

from ctypes import *

libfoo = CDLL("libfoo.so")

func1 = libfoo.func1
func2 = libfoo.func2

func1.restype = c_void_p

obj = func1() # This is where it stops with segfault

func2 (data, obj)

C代码


typedef struct
{
    int int1;
    int int2;
    float float1;
    float *arr;
    fftwf_plan fft;
} mystruct;


mystruct *func1(void)
{
    mystruct *bar = (mystruct *) malloc (sizeof(mystruct));

    bar->int1 = 1;
    bar->int2 = 2;
    bar->float1 = 1.0;
    ....

    bar->arr = (float *) fftwf_malloc (sizeof(float) * (FFT_LEN + 2));
    bar->fft = fftwf_plan_dft_r2c_1d (FFT_LEN, ch->arr, (fftwf_complex *)ch->arr, FFTW_MEASURE);

    return bar;
}

void func2 (float *data, mystruct *bar)
{
    ....
}

我也尝试创建一个 ctypes 结构对象,但仍然出现段错误。如果可能的话,我想简单地将指针传递给 func2,而不必创建 ctypes 结构。我读过几篇有类似问题的帖子,但没有一个是真正匹配的。

4

0 回答 0