我正在做一些在线练习,所以我遇到了一些问题。
我不知道我的代码哪里有问题。当我运行代码时返回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/