A
A
avion2019-12-29 14:19:30
Programming
avion, 2019-12-29 14:19:30

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

3 answer(s)
J
jcmvbkbc, 2019-12-29
@avion123678

why not allocate more memory for it?

Indeed, why not, given that this setting is in the hands of a programmer who builds (on Windows, see cl /F ) or launches (on *nix, see ulimit -s) a program?
And what, have you already learned how to free the memory allocated on the stack in an arbitrary order?

G
Griboks, 2019-12-29
@Griboks

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

C
carakan, 2020-12-30
@carakan

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 question

Ask a Question

731 491 924 answers to any question