Possible Duplicate:
C++ Which is faster: Stack allocation or Heap allocation
What is more efficient from memory allocation perspective - stack memory or heap memory? What it depends on?
Obviously there is an overhead of dynamic allocation versus allocation on the stack. Using heap involves finding a location where the memory can be allocated and maintaining structures. On the stack it is simple as you already know where to put the element. I would like to understand what is the overhead in worst case in milliseconds on supporting structures that allow for dynamic allocation?
The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free.
Because the data is added and removed in a last-in-first-out manner, stack-based memory allocation is very simple and typically much faster than heap-based memory allocation (also known as dynamic memory allocation) e.g. C's malloc .
It was more that heap allows you to know if you have enough memory to allocate (by checking the return of new) while this is not the same in stack. Note about reference is that in c++ references can't be null and have to point to an object, but pointers can be null.
Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution. Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it.
Stack is usually more efficient speed-wise, and simple to implement!
I tend to agree with Michael from Joel on Software site, who says,
It is more efficient to use the stack when it is possible.
When you allocate from the heap, the heap manager has to go through what is sometimes a relatively complex procedure, to find a free chunk of memory. Sometimes it has to look around a little bit to find something of the right size.
This is not normally a terrible amount of overhead, but it is definitely more complex work compared to how the stack functions. When you use memory from the stack, the compiler is able to immediately claim a chunk of memory from the stack to use. It's fundamentally a more simple procedure.
However, the size of the stack is limited, so you shouldn't use it for very large things, if you need something that is greater than something like 4k or so, then you should always grab that from the heap instead.
Another benefit of using the stack is that it is automatically cleaned up when the current function exits, you don't have to worry about cleaning it yourself. You have to be much more careful with heap allocations to make sure that they are cleaned up. Using smart pointers that handle automatically deleting heap allocations can help a lot with this.
I sort of hate it when I see code that does stuff like allocates 2 integers from the heap because the programmer needed a pointer to 2 integers and when they see a pointer they just automatically assume that they need to use the heap. I tend to see this with less experienced coders somewhat - this is the type of thing that you should use the stack for and just have an array of 2 integers declared on the stack.
Quoted from a really good discussion at Joel on Software site:
stack versus heap: more efficiency?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With