Cython Memoryviews - 大数组上的Seg错误?

rad*_*lus 4 python cython

即使非常小的简单整数数组也会看到奇怪的行为.

%%cython
import numpy as np
cimport cython
cimport numpy as np

def hi():
    DEF MAX = 10000000
    cdef int a[MAX],i
    cdef int[:] a_mv = a
Run Code Online (Sandbox Code Playgroud)

这会崩溃,但对较小视图的观看会影响我的观看.这不是一个明显的内存问题,因为有足够的内存可用于1000万个内存......

Bak*_*riu 7

正如Kevin在评论中提到的,问题不在于RAM,而在于堆栈.你正在堆栈上分配一个包含1000万个元素的数组,当你真的应该在堆上分配它,使用mallocet friends.即使在C中,这也会产生分段错误:

 /* bigarray.c */
int main(void) {
    int array[10000000];
    array[5000000] = 1;   /* Force linux to allocate memory.*/
    return 0;
}

$ gcc -O0 bigarray.c   #-O0 to prevent optimizations by the compiler
$ ./a.out 
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

而:

/* bigarray2.c */
#include <stdlib.h>

int main(void) {
    int *array;
    array = malloc(10000000 * sizeof(int));
    array[5000000] = 1;
    return 0;
}

$ gcc -O0 bigarray2.c
$ ./a.out 
$ echo $?
0
Run Code Online (Sandbox Code Playgroud)