首页 > 解决方案 > python ctypes返回零作为数组中的第一个元素?

问题描述

我有一个使用 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))

标签: pythoncpython-3.xnumpyctypes

解决方案


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

测试.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));
}

测试.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]

推荐阅读