+ 7

how heap is better than stack or vice-versa??

15th Feb 2017, 9:17 AM
sonali
sonali - avatar
3 odpowiedzi
+ 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.
15th Feb 2017, 1:29 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 4
@imsrk not any specific i like oll currently c++, java ,javascript
18th Feb 2017, 8:15 AM
sonali
sonali - avatar
+ 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
21st Feb 2017, 6:02 PM
Jayesh Sarvaiya
Jayesh Sarvaiya - avatar