具有2D阵列的Seg Fault(Seg Fault with 2D array)
我不知道为什么我的二维数组初始化会导致seg错误,所以我有
void viterbi_algorithm(double init[], double A[][26], double B[][2], int obs[], int mostLikelyStates[]){ cout << "start" << endl; double table1[26][68000]; double table2[26][68000]; ..如果我评论出这两张桌子,一切都会好的。 我要求太多回忆吗?
我运行gdb时的错误
Program received signal SIGSEGV, Segmentation fault. ___chkstk_ms () at /usr/src/debug/gcc-4.8.1-3/libgcc/config/i386/cygwin.S:146 146 orq $0x0, (%rcx) /* probe there */I'm not sure why my 2d array initialization causes a seg fault, so I have
void viterbi_algorithm(double init[], double A[][26], double B[][2], int obs[], int mostLikelyStates[]){ cout << "start" << endl; double table1[26][68000]; double table2[26][68000]; ..If I comment out the two tables, everything will be okay. Am I asking for too much memories?
My error when I ran gdb
Program received signal SIGSEGV, Segmentation fault. ___chkstk_ms () at /usr/src/debug/gcc-4.8.1-3/libgcc/config/i386/cygwin.S:146 146 orq $0x0, (%rcx) /* probe there */最满意答案
使用关键字static定义这些数组
static double table1[26][68000]; static double table2[26][68000];在这种情况下,它们将被分配在静态存储器中。
另一种方法是使用标准容器std::vector 。 例如
std::vector<std::vector<double>> table1( 26, std::vector<double>( 68000 ) ); std::vector<std::vector<double>> table2( 26, std::vector<double>( 68000 ) );您还可以使用关键字static定义它们
static std::vector<std::vector<double>> table1( 26, std::vector<double>( 68000 ) ); static std::vector<std::vector<double>> table2( 26, std::vector<double>( 68000 ) );Define these arrays using keyword static
static double table1[26][68000]; static double table2[26][68000];In this case they will be allocated in the static memory.
The other approach is to use standard container std::vector. For example
std::vector<std::vector<double>> table1( 26, std::vector<double>( 68000 ) ); std::vector<std::vector<double>> table2( 26, std::vector<double>( 68000 ) );You also can define them with keyword static
static std::vector<std::vector<double>> table1( 26, std::vector<double>( 68000 ) ); static std::vector<std::vector<double>> table2( 26, std::vector<double>( 68000 ) );更多推荐
发布评论