【发布时间】:2019-11-08 02:53:55
【问题描述】:
我正在尝试将 Numpy 数组传递给 C,但在 Windows 和 Linux 中得到不同的结果。
在 Python 中
import platform
import numpy as np
import ctypes
if platform.system() == 'Windows':
c_fun = np.ctypeslib.load_library("/mypath/c_fun.dll", ".").c_fun
else: # Linux
c_fun = np.ctypeslib.load_library("/mypath/c_fun.so", ".").c_fun
c_fun.argtypes = [np.ctypeslib.ndpointer(dtype=np.int, ndim=2, flags="C_CONTIGUOUS"), ctypes.c_int, ctypes.c_int]
array = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
rows, cols = array.shape
c_fun(array, rows, cols)
在 C 中
void c_fun(int* array, int rows, int cols)
{
for (int i = 0; i < rows * cols; i++)
printf("%d ", array[i]);
}
当我在 Windows 中运行程序时,输出为“0 1 0 0 1 0 0 1 0”,它运行良好。
但是在Linux中,输出是“0 0 1 0 0 0 0 0 1”,为什么?
【问题讨论】: