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,为什么没人这么做(还是有,我不知道)
|
|