堆栈内存有限制吗?

编程入门 行业动态 更新时间:2024-10-27 01:27:58
本文介绍了堆栈内存有限制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

堆栈内存有限制吗?

本文发布于:2023-10-17 11:04:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   内存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!