+ 7
how heap is better than stack or vice-versa??
3 Respostas
+ 5
Variables on the stack are more efficient than variables on the heap. The reason is that variables on the heap can only be accessed via pointers (or references in Java/C#), so you pay the cost of the indirection of the pointer/reference each time you use the variable.
You cannot create dynamically sized arrays on the stack - they have to be statically sized arrays. So in this regard the heap is better.
The heap is practically unlimited in size whilst the stack is fixed in size (the app can ask for extra heap space from the OS when its heap space gets low; it cannot do so for the stack).
In short - the heap is more flexible, but the stack is better performing.
+ 4
@imsrk not any specific i like oll currently c++, java ,javascript
+ 2
if I am correct both heap and stack are part of ram memory, stack is used as we make function calls and use local variable and is automatically freed. whereas heap is used to dynamically allocate memory at run time which needs to be freed exclusively
it depends on application which one is more useful