我知道这个问题,但我一直在寻找一种更简单的方法来从 C 数组生成 2d 内存视图。由于我是 C 和 Cython noobie,有人可以解释一下为什么像
cdef int[:, :] get_zeros(int d):
# get 2-row array of zeros with d as second dimension
cdef int i
cdef int *arr = <int *> malloc(sizeof(int) * d)
for i in range(d):
arr[i] = 0
cdef int[:, :] arr_view
arr_view[0, :] = <int[:d]>arr
arr_view[1, :] = <int[:d]>arr
return arr_view
不行吗?
编译时我得到Cannot assign type 'int[::1]' to 'int'
错误。这是否意味着 2d memview 被第一个 assign 语句折叠到 1d 或者是因为 memoryviews 需要连续块等?