Difference Between Stack and stack

Source: Internet
Author: User

Difference Between Stack and stack

The difference between heap and stack is first explained in a metaphor:

Stack usage: Just like we go to a restaurant to eat, just order food (send an application), pay, and eat (use). If we are full, we can leave without the need to cut and wash dishes.

It is quick but has a low degree of freedom.

Use heap: it is like making your favorite dishes by yourself. It is troublesome, but it suits your taste and has a high degree of freedom.

1. Application Method

STACK: automatically allocated by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.

Heap: the programmer needs to apply for it and specify the size. In c, the malloc Function

For example, p1 = (char *) malloc (10 );

Use the new operator in C ++

For example, char * p2 = new char [10]; // but note that p1 and p2 are in the stack.

2. system response after application

STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.

Heap: the operating system has a linked list that records the idle memory address. When the system receives the application from the program, it traverses the linked list to find the first one that is larger than the requested space.

Heap node, and then delete the node from the idle node linked list, and allocate the space of the node to the program. In addition, for most systems, the memory will be empty

In this way, the free statement in the code can correctly release the memory space. In addition

The size is not necessarily equal to the applied size. The system automatically places the excess part in the idle linked list.

3. Application size limit

STACK: different compilation systems have different types. For example, in Windows, a stack is a data structure extended to a low address and a continuous memory area. This sentence

It means that the stack top address and the maximum stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (OR 1 MB ).

If the requested space exceeds the remaining space of the stack, overflow is displayed. Therefore, the space available from the stack is small.

Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses a linked list to store idle memory addresses, which is naturally discontinuous,

The traversal direction of the linked list is from low address to high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is quite good.

Active and large.

4. Comparison of Application Efficiency

STACK: the stack is automatically allocated by the system, and the speed is fast. But programmers cannot control it.

Heap: Memory allocated by new. It is generally slow and prone to memory fragments. However, it is most convenient to use.

5. Storage content in heap and stack

STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), followed by

Parameters, in most C compilers, the parameters are from right to left into the stack, and then local variables in the function. Note that static variables are not included in the stack. When this letter

After the number call is complete, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function.

This point continues to run.

Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.

 

Related Article

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.