Heap/stack comparisons and malloc/new dynamic memory opening

Source: Internet
Author: User

Heap vs. Stack comparison:
1. How to Apply
(1) stack (SATCK): Automatically assigned by the system.
(2) Heap: Requires the programmer to apply (c: Call Malloc,realloc,calloc request free to release), and specify the size, and released by the programmer. Easy to generate memory leaks.


2. Restrictions on size of applications
(1) Stack: The stack in Windows is the data structure that extends to the bottom address, which is a contiguous area of memory (it grows in the opposite direction as the memory is growing). The size of the stack is fixed. If the requested space exceeds the remaining space on the stack, overflow will be prompted.
(2) Heap: A heap is a data structure with a high address extension (it grows in the same direction as the memory) and is a discontinuous area of memory. This is due to the fact that the system uses a linked list to store the free memory address , which is naturally discontinuous, while the traversal direction of the list is from the bottom address to the high address. The size of the heap is limited by the valid virtual memory in the computer system.


3. System response:
(1) Stack: As long as the stack of space is larger than the application space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow.
(2) Heap: First of all should know that the operating system has a record of the free memory address of the list, but the system receives the application of the program, will traverse the list, the first space is larger than the requested space of the heap node, and then delete the node from the free list, and the node's space allocated to the program, in addition, The size of this allocation is recorded at the first address in this memory space, so that the free statement in the code can properly release the memory space. In addition, the size of the found heap node is not necessarily exactly equal to the size of the request, and the system automatically re-places the extra portion into the idle list.
Note: For the heap, for the heap, the 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.


4. Application efficiency
(1) The stack is automatically assigned by the system and is fast. But programmers can't control it.
(2) Heap is the memory allocated by malloc, the general speed is relatively slow, and easy to produce fragments, but the most convenient to use.


(1) stack: When a function is called, the address of the next statement in the main function of the first stack, followed by the parameters of the function, the arguments are in the stack from right to left, and then the local variables in the function. Note: Static variables are not in the stack.

(2) heap: It is generally the size of a heap that is stored in the head of a heap with a single byte.


6. Access efficiency
(1) heap: char *s1=" Hellow tigerjibo "; is the that is determined on compilation.
(2) stack: char s1[]=" Hellow tigerjibo "; is the value assigned at run time. Using arrays is faster than using pointers, and pointers need to be relayed in the EDX register in the underlying assembly, while arrays are read on the stack.
supplement:


7. Allocation Method:
(1) The heap is dynamically allocated and there is no statically allocated heap.
(2) There are two ways to allocate stacks: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. Dynamic allocations are allocated by the ALLOCA function, but the dynamic allocation of stacks is different from the heap. Its dynamic allocation is released by the compiler without the need for manual implementation.

A comparison of dynamically allocated memory on the heap by C + +

Heap/stack comparisons and malloc/new dynamic memory opening

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.