【发布时间】:2019-06-19 01:22:24
【问题描述】:
当我使用 Visual C++ 在 Windows 上编译和运行这个简单的程序时崩溃:
#include <stdio.h>
void foo()
{
printf("function begin\n");
int n[1000000];
for(long int i = 0; i < 1000000; i++)
{
n[i] = 2;
}
printf("function end\n");
}
int main()
{
printf("hello\n");
foo();
printf("end of the program\n");
}
我用cl bug.c编译。
在这种情况下,控制台只显示:
C:\Users\senss\Desktop>bug
hello
但是,当我将 1 000 000 值更改为 100 000 时,没有问题:
C:\Users\senss\Desktop>bug
hello
function begin
function end
end of the program
谢谢!
【问题讨论】:
-
堆栈是有限资源。在 Windows 上,默认的进程策略只有一个 MiB。
-
将
int n[1000000];更改为int *n = malloc(1000000 * sizeof(int)); if (!n) return; -
欢迎来到 Stack Overflow! [双关语] :)
-
感谢您的帮助和解释,我会修复它!
标签: c windows memory tabs allocation