【发布时间】:2020-01-10 17:10:23
【问题描述】:
我需要在一项任务中制作一个大数组(超过 10^7)。
我发现如果我在 int main 中执行此操作,代码将无法正常工作(程序将在执行 cout“进程返回 -1073741571 (0xC00000FD)”之前退出)。
如果我在外面做,一切都会奏效。
(我正在使用 Code::Blocks 17.12)
// dont work
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7;
int main() {
int a[N];
cout << 1;
return 0;
}
// will work
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7;
int a[N];
int main() {
cout << 1;
return 0;
}
所以我有问题:
-为什么会这样?
-如何在 int main() 中定义数组? (实际上,如果我在 int main() 中使用相同大小的向量,一切都会正常工作,这很奇怪)
【问题讨论】:
-
在典型的实现中,自动分配在大小有限的堆栈上。全局变量和动态变量在堆上分配。
-
@stark 全局变量未在堆上分配。
标签: c++ memory memory-limit