我遇到了以下ctypes的奇怪行为。当我通过ctypes将numpy数组转换为int指针时,会丢失一些值并添加额外的零。更具体地说,当我使用以下代码转换numpy数组时
from numpy import *
import ctypes
from numpy.ctypeslib import ndpointer
x = arange(1, 10)
xInt = x.ctypes.data_as(ctypes.POINTER(ctypes.c_int * len(x)))
xLong = x.ctypes.data_as(ctypes.POINTER(ctypes.c_long * len(x)))
然后我得到以下令人困惑的结果:
print [i for i in xInt.contents]
[1, 0, 2, 0, 3, 0, 4, 0, 5]
print [i for i in xLong.contents]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
发生了什么事?