Definition and difference of heap and stack

Source: Internet
Author: User

Stack: When executing a function, the storage units of local variables within the function can be created on the stack and are freed automatically at the end of the function execution. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but with limited allocation capacity.

(That is, the object is stored)

Heap: Is the memory blocks allocated by new, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer is not released, the operating system will automatically recycle after the program finishes. (that is, the base data type is stored)

What is the difference between heaps and stacks?

1. Different management methods;

2. The size of the space is different;

3. can produce different fragments;

4. Different direction of growth;

5. Different distribution methods;

6. Distribution efficiency is different;

1. Management : For the stack, is automatically managed by the compiler, without our manual control, for the heap, release work by the programmer control, easy to produce memory leak.

2. Space size : Generally speaking, in the 32-bit system, heap memory can reach 4G space, from this point of view heap memory is almost no limit.

But for the stack, generally there is a certain amount of space. We can modify: Open the project. The Operation menu is as follows: Project->setting->link, select output in category, Then set the stack's maximum value and commit in the reserve. Note : The reserve minimum value of 4byte;commit is reserved in the virtual memory of the page file, it is set to a large size stack will open up a larger value, may increase the memory overhead and startup time.

3. fragmentation problem . For the heap, frequent new/delete will inevitably cause the memory space discontinuity, resulting in a large number of fragments, so that the program efficiency is reduced. For the stack, there is no problem because the stack is advanced after the queue, they are so one by one correspondence, So that there will never be a memory block ejected from the middle of the stack, and before he pops up, the contents of the last stack above him have been ejected.

4. direction of growth : For the heap, the direction of growth is upward, that is, to the memory address of the direction of increase, for the stack, its growth direction is downward, is toward the memory address of the direction of growth.

5. Allocation method: The heap is dynamically allocated and there is no statically allocated heap. There are 2 ways to allocate the stack: static allocation and dynamic allocation. Static allocations are done by the compiler, such as local variables. The dynamic allocation is assigned by the ALLOCA function, but the dynamic allocation of the stack is different, His dynamic assignment was released by the compiler without our manual implementation.

6. Allocation efficiency : The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, the stack has the special instruction execution, which determines the efficiency of the stack is high. The heap is provided by the C/S function library, and its mechanism is very complex. , for example, in order to allocate a piece of memory, the library function searches the heap memory for an available space of sufficient size, and if there is not enough control (possibly due to too much memory fragmentation), it is possible to call system functions to increase the memory control of the program data segment, thus having the opportunity to allocate enough memory. And then return. Obviously, the heap is much less efficient than the stack.

From here we can see that heap and stack, due to the use of a large number of new/delete, prone to large amounts of memory fragmentation, hesitation without special system support, low efficiency; hesitation can trigger a switchover of the user state and kernel mentality, and the cost of memory applications becomes more expensive. So stacks are the most widely used in the program, Even if the invocation of a function is done using the stack, the parameters in the function call, the return address, the EBP and the local variables are all stored as stacks. So, we recommend that you try to use stacks.

Summary : The difference between heap and stack can be seen in the following analogy:

Use the heap like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but the freedom of small

Using the heap is like doing your own favorite dishes, more trouble, but more in line with their own tastes, and great freedom.

Definition and difference of heap and stack

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.