我正在处理其中一个线程.一个程序崩溃了,因为它在函数中本地声明了一个 10^6 的数组.
I was going through one of the threads. A program crashed because it had declared an array of 10^6 locally inside a function.
给出的原因是堆栈上的内存分配失败导致崩溃.
Reason being given was memory allocation failure on stack leads to crash.
当全局声明相同的数组时,它运行良好.(堆上的内存保存了它).
when same array was declared globally, it worked well.(memory on heap saved it).
现在,让我们假设,栈向下增长,向上堆.
Now for the moment, let us suppose, stack grows downward and heap upwards.
我们有:
现在,我相信如果堆栈分配失败,它也必须在堆上失败.
Now , I believe that if there is failure in allocation on stack, it must fail on heap too.
所以我的问题是:堆栈大小有限制吗?(越界导致程序崩溃).还是我错过了什么?
So my question is: is there any limit on stack size? (crossing the limit caused the program to crash). Or am I missing something?
推荐答案这一切都取决于您使用的语言和编译器.但是用例如 C 或 C++ 编译的程序在程序启动时分配一个固定大小的堆栈.堆栈的大小通常可以在编译时指定(在我的特定编译器上,它默认为 1 MB).
This all depends on what language and compiler you use. But programs compiled with for instance C or C++ allocate a fixed size stack at program startup. The size of the stack can usually be specified at compile time (on my particular compiler it default to 1 MB).
更多推荐
堆栈内存有限制吗?
发布评论