5

我正在尝试为将输入作为指针的旧 C 程序创建一个 Python 包装器。此时,我可以让程序运行,但不知道如何取回指定指针处的值。

这是一个简化的 C 脚本:

#include <stdlib.h>

void cprogram(double *wts, double *res, int *kks, int n, double *ex) {
  int m;
  m=n+1;
  res[0]=1.0;
  kks[0]=1.0;}

这是我简化的 Python 代码:

from ctypes import *
import sys

libc = CDLL("src/program.so")  

class CONTEXT(Structure):
  _fields_ = [
                ("wts", POINTER(c_double)), //tried just to see if it would work
                ("res", c_double),
                ("kks", c_int),
                ("n", c_int),
                ("ex", c_double)]

def runner():
    kk = (1,2,3)
    n = 3
    mm = n + 1
    wts = (c_double * n)(1, 1, 1)
    res = (c_double * mm)(0)
    kks = (c_int * len(kk))(*kk)
    n = c_int(n)
    ex = c_double(0)

    libc.cprogram.restype = POINTER(CONTEXT)

    tmp = libc.cprogram(wts, res, kks, n, ex)

runner()

我已经尝试过类似的命令print tmp[1].wts[1]print tmp[2]但这只打印内存地址而不是值(或非常小的不正确的值,如 2.15880221124e-314)。我希望能够返回 wts 的值列表。

4

1 回答 1

3

您的 C 函数返回void,而不是CONTEXT *. 因此,您的代码只是将随机未初始化的内存转换为 a CONTEXT *,然后尝试取消它。

最重要的是,即使它确实CONTEXT通过引用返回了一个对象,tmp[1]也会尝试在该对象之后取消引用内存,因此它仍然是垃圾。

当您尝试将随机内存解释为双精度时,您将获得段错误或值,例如2.15880221124e-314如果您幸运的话 - 如果您不走运,您将获得看起来正确但仍然是随机值,例如0.0.


同时,由于你的 C 函数修改了它的参数,你不需要在这里做任何花哨的事情。只需使用您传入的变量即可。

所以:

def runner():
    kk = (1,2,3)
    n = 3
    mm = n + 1
    wts = (c_double * n)(1, 1, 1)
    res = (c_double * mm)(0)
    kks = (c_int * len(kk))(*kk)
    n = c_int(n)
    ex = c_double(0)

    libc.cprogram.restype = None

    libc.cprogram(wts, res, kks, n, ex)

    print wts[1]

这有效,并打印出1.0.


如果您的 C 函数确实返回了一个与您的声明CONTEXT匹配的结构数组,那么这一切都可以正常工作。ctypes例如:

#include <stdlib.h>

typedef struct {
  double *wts;
  double res;
  int kks;
  int n;
  double ex;
} CONTEXT;

CONTEXT *cprogram(double *wts, double *res, int *kks, int n, double *ex) {
  int m;
  m=n+1;
  res[0]=1.0;
  kks[0]=1.0;

  CONTEXT *contexts = malloc(sizeof(CONTEXT) * 4);
  for (int i=0; i!=4; ++i) {
    double *wtsses = malloc(sizeof(double) * 5);
    for (int j=0; j!=4; ++j) {
      wtsses[j] = i + j;
    }
    CONTEXT context = { wtsses, *res, *kks, m, *ex };
    contexts[i] = context;
  }
  return contexts;
}

编译这个,运行你现有的 Python 脚本并添加一个print tmp[1].wts[1],它会打印出2.0.


最后,为了您的跟进,首先让我们将 C 代码更改为 take int *n*n输入在哪里:

void cprogram(double *wts, double *res, int *kks, int *n, double *ex) {
  int m;
  m=*n+1;
  res[0]=1.0;
  kks[0]=1.0;}

现在,要从 Python 调用它,您必须创建一个c_int并传递一个指向它的指针。既然你已经在做前半部分了(这在之前是不需要的——只是设置argtypes了......但现在是必要的,这很方便),它只是一个单行更改:

libc.cprogram(wts, res, kks, pointer(n), ex)

如果n是输入输出参数,这甚至可以工作。


但实际上,您根本不需要从 Python 中查看指针对象;你唯一要做的就是创建它以传递给函数,然后让它被收集。要在不创建 ctypes 指针对象的情况下传递 C 指针(即使n是 in-out 参数也可以使用),请使用byref

libc.cprogram(wts, res, kks, byref(n), ex)
于 2013-07-17T23:44:03.507 回答