开发者

Why is there a stack and a heap?

开发者 https://www.devze.com 2023-03-29 13:48 出处:网络
Why do assembly languages use both a stack and a heap? They s开发者_如何学运维eem redundant.They\'re not redundant. Each of them has strengths and weaknesses: A stack is faster if used right, because

Why do assembly languages use both a stack and a heap? They s开发者_如何学运维eem redundant.


They're not redundant. Each of them has strengths and weaknesses: A stack is faster if used right, because memory allocation is trivial (push / pop). The downside is that you can only add and remove items at the top (hence the name, stack). Also, total stack space is limited, and when you run out, you have a... well, stack overflow. The heap, by contrast, allows random allocation and deallocation, and you can store large amounts of data there, but the downside is that allocation carries more overhead - for each allocated block of memory, a suitable free portion must be found, and in the long run, fragmentation of the free space needs to be avoided, and the system must track where the free blocks are.

You use the stack to pass around small short-lived values, e.g. local counter variables, function arguments, return values, etc.; these lend themselves to push/pop allocation style. For larger or long-lived data structures, you use the heap.


You could certainly construct a computing system that utilised either one of them as its only memory model. However, they both have rather different properties each with its own good and bad points. Most systems utilise both so as to get the benefits from each of them.

Stacks

A stack can be thought of as a pile of plates, you write a value on a plate and put it on the top of the stack this is called a push operation and stores a value on the stack. You can obviously also remove the top plate from the stack this is called a pop operation. But new allocations must always be at the top of the stack.

The stack tend to be used for local variables and passing values between functions. Generally stacks have the following awesome properties:

  • Requires only a handful of pointers to manage
  • Very easy to implement in hardware, most processors have built in hardware support for a stack making it even faster.
  • Very quick to allocate memory

The problem with the stack comes from the fact items can only be added/removed from the top of the stack. Now this makes great sense when traversing up and down through function calls: pop functions inputs from the stack, allocate space for local variables on the stack, run function, clear local variables from the top of the stack and push the return value onto the stack. If on the other hand I want to allocate some memory and say pass it to another thread or in general free it far away from where it was allocated all of a sudden I have a problem, the stack is not in the correct position when I want to free the memory.

You could say the stack facilitates fast sequential memory allocation.

Heap

Now the heap is different each allocation is generally tracked separately. This causes a lot of overhead for allocations and deallocations, but each one can be handled independently of other memory allocations, well until you run out of memory.

There are numerous algorithms for accomplishing this and it is probably a bit unwise to twitter on about them here but here is a link that talks about a few good simple heap allocation algorithms: Alternatives to malloc and new

So the heap facilitates random memory allocation but this comes with a runtime penalty, however that penalty is often small that what would be incurred if you had to handle the situation using just the stack.


It is about the memory handling and managing. There are different type of registers of x86 architectures. There are possibilities of hardware supported memory management on x86 architecture and so on.

Stack is used by instruction pointer, Heap is for data segment in some applications.

To read more I advice you read the following links:

  • http://en.wikipedia.org/wiki/Data_segment
  • http://en.wikipedia.org/wiki/X86_memory_segmentation

"A memory model allows a compiler to perform many important optimizations" - Wikipedia

0

精彩评论

暂无评论...
验证码 换一张
取 消