gpt4 book ai didi

python - Ctypes 返回数组

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:38 27 4
gpt4 key购买 nike

我正在尝试为 C 中的数组排序函数创建一个 python 包装器。C 获取数组,按从小到大对整数进行排序,然后返回数组。但是,当我运行它时,出现错误:

Traceback (most recent call last):
File "sortarray.py", line 25, in <module>
newarray = sortArray(array)
File "sortarray.py", line 8, in sortArray
libsortarray.sortArray.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_int))
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x7f84484280e0, sortArray): symbol not found

python :

import ctypes

libsortarray = ctypes.CDLL('libsortarray.so')

def sortArray(array):
global libsortarray
libsortarray.sortArray.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_int))
arraySize = len(array)
array_type = ctypes.c_int * arraySize
result = libsortarray.sortArray(ctypes.c_int(arraySize), array_type(*array))
return result


file = open('bigarray.txt', 'r')
#Bigarray.txt is just 10,000 lines each with a single integer
array = []
arraySize = 10000
for i in range(0,arraySize):
array.append(int(file.readline()))
file.close()

newarray = sortArray(array)
print newarray

还有 libsortarray 函数

int* sortArray(int, int*);

int* sortArray(int arraySize, int* array) {
int temp, i, j;
for (i=0; i<arraySize; i++)
for (j=i+1; j<arraySize; j++)
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}

最佳答案

如果源是 C++,则需要将函数声明为 extern "C"int *sortArray(int, int *)。此外,当函数返回指针时,将 restype 属性设置为指针类型,在本例中为 sortArray.restype = POINTER(c_int)。否则,在 64 位进程中,地址会被截断为 32 位,从而创建一个错误的指针,在访问时可能会出现段错误。此外,这更像是一个风格问题,声明 global libsortarray 和手动包装 arraySizec_int(arraySize) 都是不必要的困惑。

也就是说,库函数对数组进行了就地排序,因此没有理由返回任何东西,即只需将返回类型设置为 void。下面是一个实现此建议修改的示例。

排序数组.cpp:

extern "C" void sortArray(int, int *);

void sortArray(int arraySize, int *array)
{
int temp, i, j;
for (i = 0; i < arraySize; i++)
for (j = i + 1; j < arraySize; j++)
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

// g++ -shared -fPIC -o libsortarray.so sortarray.cpp

排序数组.py

import ctypes

libsortarray = ctypes.CDLL('./libsortarray.so')

libsortarray.sortArray.restype = None
libsortarray.sortArray.argtypes = (ctypes.c_int,
ctypes.POINTER(ctypes.c_int))

def sort_array(array):
"""Return a sorted copy of the input array or sequence."""
array_size = len(array)
array = (ctypes.c_int * array_size)(*array)
libsortarray.sortArray(array_size, array)
return array

if __name__ == '__main__':
seq = [7, 0, 8, 4, 3, 6, 9, 1, 5, 2]
print 'Unsorted Array:\n', seq
print 'Sorted Array:\n', sort_array(seq)[:]

输出:

Unsorted Array:
[7, 0, 8, 4, 3, 6, 9, 1, 5, 2]
Sorted Array:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

关于python - Ctypes 返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29015606/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com