The difference between the heap area and the stack area

Source: Internet
Author: User

For the stack, is automatically managed by the compiler, without our manual control, for the heap, the release of the work programmer control, easy to produce memory Leak.

Application Size:

Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, a compiler to determine a constant), if the application of space beyond the stack of space when the overflow. Therefore, the space to get the stack is small.

Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. Heap laughter is limited by the amount of virtual memory available in the computer system. Thus, the space of the heap is more flexible and relatively large.

problem with fragmentation:  

For the heap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory quickly ejected from the stack.

allocation method:  

The heap is dynamically allocated and there are no statically allocated heaps. Stacks are allocated in two ways: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is assigned by the Alloc function, but the dynamic allocation of the stack is different from the heap, and his dynamic allocation is freed by the compiler without our manual implementation.

Allocation efficiency:

Stack is the data structure provided by the machine system, the computer will provide support on the underlying stack, allocate the address of the special register storage stack, and the stack stack has special instruction execution, which determines the efficiency of the stack is high. The heap is provided by the C + + function library, and his mechanism is very complex.

The difference between the heap area and the stack area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.