堆栈内存有限制吗? [英] Is there any limit on stack memory?

查看:34
本文介绍了堆栈内存有限制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理其中一个线程.一个程序崩溃了,因为它在函数中本地声明了一个 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).

这篇关于堆栈内存有限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆