c++ - 使用二维数组。段错误 - C++

标签 c++ multidimensional-array

我正在做一些在线练习,所以我遇到了一些问题。 我不知道我的代码哪里有问题。当我运行代码时返回Segmentation fault! 我的代码如下:

#include <iostream>

using namespace std;


int main() {
    int n, q, buff;
    int k[100000], i[100000], j[100000], a[100000][300000];
    int c;



   cin >> n >> q;


    for(buff=0; buff<n; buff++) {
        cin >> k[buff];
        //array
        for(c = 0;c < k[buff]; c++) {
           cin >> a[buff][c];  // <-- I think problem is here!
       }
    }


    for(buff = 0; buff < q; buff++) {
        cin >> i[buff];
    }


    for(buff = 0; buff < q; buff++) {
        cin >> j[buff];
    }   


    return 0;
}

请帮帮我!对不起我的英语。

最佳答案

在堆栈中使用如此大的数组会导致堆栈溢出。参见 Getting a stack overflow exception when declaring a large array .

改用std::vector

std::vector<int> k{100000};
std::vector<int> i{100000};
std::vector<int> j{100000};
std::vector<std::vector<int>> a{100000, std::vector<int>{300000}};

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

相关文章:

c++ - 模板类重载赋值运算符自赋值测试VC++ Express 2010报错

c++ - STL 对输入与 STL 映射

无法使用 c 将值存储在矩阵中

c++ - 在结构内部定义时初始化 Boost.MultiArray?

php - 如何在不显式编写循环运算符的情况下展平一个简单的数组?

C++:函数模板指针的 std::vector

c++ - Enterprise Architect 中的模型模板功能

c++ - 生成具有它们之间差异的随机坐标

c - 将二维数组传递给 C 中的函数?

c - "Wrong number of indices inside []": Why?