【问题标题】:python ctypes returns zero as first element in array?python ctypes返回零作为数组中的第一个元素?
【发布时间】:2018-07-12 14:26:04
【问题描述】:

我有一个使用 ctypes 的简单 python 脚本,它将数组指针传递给外部函数。然后,外部函数只是意味着将相同的数组传递回 python(这意味着作为一个简单的单元测试,以确保 python 和外部函数都使用相同的值)。函数原型如下所示:

void Array_tmp(int32_t Array[], int32_t *len, int32_t Array2[])

python 脚本如下所示:

from ctypes import *
import numpy as np
import numpy.ctypeslib
import os

dll = 'array_test.dll'

loadlib = cdll.LoadLibrary(dll)

arr = np.array([1,2,3,4,5])
length = len(arr)

c_arr = np.ctypeslib.as_ctypes(arr)
clen = c_int32(length)
p_clen = pointer(clen)
c_arr_ptr = cast(c_arr, POINTER(c_double))
loadlib.Array_tmp.argtypes = [type(c_arr_ptr), type(p_clen)]
loadlib.Array_tmp.restype = type(c_arr)

g = loadlib.Array_tmp(c_arr_ptr, p_clen)
print(g)
np_arr_g = np.ctypeslib.as_array(g)
print(np_arr_g)

脚本的输出如下所示:

<numpy.ctypeslib.c_long_Array_5 object at 0x044A7AD0>
[0 2 3 4 5]

为什么数组的第一个元素显示为零而不是一个,而其他所有元素都是正确的?

编辑:将行 c_arr_ptr = cast(c_arr, POINTER(c_double)) 更改为 c_arr_ptr = cast(c_arr, POINTER(c_int32)) 对输出没有影响,除了内存地址可能略有变化。

【问题讨论】:

  • everything 这个代码有问题。您的函数有 3 个参数,而不是 2 个,并且它返回 void 而不是“数组”,无论它是什么。此外,如果函数采用指向 int32_t 的指针,而您提供指向 double 的指针,则行为完全未定义。
  • 所以第三个参数应该是函数的输出。如果输出在技术上无效,还有其他方法可以从 python 访问它吗?

标签: python c python-3.x numpy ctypes


【解决方案1】:

提供的代码存在很多问题。这是一个工作示例:

test.c

#include <memory.h>
#include <inttypes.h>

#define API __declspec(dllexport) /* Windows-specific export */

/* FYI: No reason for 'len' to be a pointer */
API void Array_tmp(int32_t Array[], int32_t *len, int32_t Array2[])
{
    memcpy(Array2,Array,*len * sizeof(int32_t));
}

test.py

from ctypes import *
import numpy as np

dll = CDLL('test')

# Good idea to declare the functions arguments and return type fully for error checking.
func = dll.Array_tmp
func.argtypes = POINTER(c_int32),POINTER(c_int32),POINTER(c_int32)
func.restype = None

# Create input array and output array of same size.
arr = np.array([1,2,3,4,5])
out_arr = np.zeros(arr.shape,dtype=np.int32)

# Get everything as a ctypes object
c_arr = np.ctypeslib.as_ctypes(arr)
c_out_arr = np.ctypeslib.as_ctypes(out_arr)
clen = c_int32(len(arr))

func(c_arr,byref(clen),c_out_arr)

# Original output array of zeros is now modified
print(out_arr)

输出:

[1 2 3 4 5]

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 2011-07-12
    • 1970-01-01
    • 2021-03-29
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    相关资源
    最近更新 更多