gpt4 book ai didi

c++ - 二维数组的段错误

转载 作者:行者123 更新时间:2023-11-30 01:54:27 24 4
gpt4 key购买 nike

我不确定为什么我的二维数组初始化会导致段错误,所以我有

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 */

最佳答案

使用关键字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 ) );

关于c++ - 二维数组的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21984999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com