具有2D阵列的Seg Fault(Seg Fault with 2D array)

编程入门 行业动态 更新时间:2024-10-23 05:40:10
具有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 ) );

更多推荐

本文发布于:2023-08-05 08:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1430209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   Seg   array   Fault

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!