Answer the question
In order to leave comments, you need to log in
Stack and heap (memory)?
Why, relative to the heap, is the stack so small, 1 - 8 MB on average? The stack is faster, why not allocate more memory for it?
Answer the question
In order to leave comments, you need to log in
why not allocate more memory for it?
ulimit -s
) a program?You can allocate as much memory as you need, but keep in mind that:
1. the larger the stack, the slower it is
2. 99% of programs do not use even a small stack completely
3. storing large variables on the stack is inefficient
You need to understand why the stack is effective. This is not because memory was somehow allocated in a special way, but because it is used in a special way.
There lie only relatively small local variables. You only work with the top of the stack. Therefore, all of it is stored in the cache memory. If the cache is deliberately inflated (for example, instead of dynamic arrays in the heap, an array on the stack with a margin is made), it will lose its effectiveness.
Since the variables are in a row on the stack, we cannot implement new there. And without dynamic arrays, a lot of memory is not needed. The value selected by default is usually enough for all occasions. It's worth increasing only if it's really not enough. But this can only happen, for example, when working with deep graphs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question