由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 为什么会有recursion stack overflow这个问题?
相关主题
static variable存在heap还是stack?请问一个关于 cost of pointer的问题
请教windows 7 怎么增加堆栈上限"brk()" 和 mmap() 有什么区别? (转载)
嵌入式系统用什么sorting算法比较好?reverse LL recursively
how much slower: heap vs stack memory allocation?怎样高效管理内存?
stack/heap corruption树的前序遍历
问一个private destructor的问题array allocation in c
[合集] 谁给个stack-based allocation 的C++的例子?关于thread的stack
How solid it is: c++/c# call Matlab librarya question about std::stack
相关话题的讨论汇总
话题: stack话题: heap话题: recursion话题: variables话题: memory
进入Programming版参与讨论
1 (共1页)
z***t
发帖数: 14
1
所有的recursion algorithm都可以写成iterative algorithm + stack.这个时候的
stack是在heap上的。而一个程序stack的大小远小于heap的大小,既然这样,compiler
为什么不遇到recursion call就在heap上做?
c******o
发帖数: 1277
2
first we need to ask why we have heap and stack, obviously you can implement
one using the other.
they just different, and stack is faster mostly if you are ok with its cons
copy from else where:
Stack
very fast access
don't have to explicitly de-allocate variables
space is managed efficiently by CPU, memory will not become fragmented
local variables only
limit on stack size (OS-dependent)
variables cannot be resized
Heap
variables can be accessed globally
no limit on memory size
(relatively) slower access
no guaranteed efficient use of space, memory may become fragmented over time
as blocks of memory are allocated, then freed
you must manage memory (you're in charge of allocating and freeing variables)
variables can be resized using realloc()
z***t
发帖数: 14
3
受教了,其实我不明白的是这个compiler可以设计的几乎完全避免recursion
stackoverflow,为什么没人这么做(还是有,我不知道)
p***o
发帖数: 1252
4
You can change the stack size as you like so you cannot simply say
stack size is much smaller than heap size.
Stack size is small by default though, which allows OS to quickly
catch bugs related to termination of your recursive functions.
If your compiler moves most of the things to the heap, you may need
to wait for quite a while before your program fails.

【在 z***t 的大作中提到】
: 受教了,其实我不明白的是这个compiler可以设计的几乎完全避免recursion
: stackoverflow,为什么没人这么做(还是有,我不知道)

1 (共1页)
进入Programming版参与讨论
相关主题
a question about std::stackstack/heap corruption
问一个C++ set和unordered_set iterator的问题问一个private destructor的问题
那些用buffer overflow来attack的人是怎么计算出要覆盖的内存地址的阿?[合集] 谁给个stack-based allocation 的C++的例子?
C语言的变量都一定要放在stack上吗?How solid it is: c++/c# call Matlab library
static variable存在heap还是stack?请问一个关于 cost of pointer的问题
请教windows 7 怎么增加堆栈上限"brk()" 和 mmap() 有什么区别? (转载)
嵌入式系统用什么sorting算法比较好?reverse LL recursively
how much slower: heap vs stack memory allocation?怎样高效管理内存?
相关话题的讨论汇总
话题: stack话题: heap话题: recursion话题: variables话题: memory