Difference Between Stack and stack

Source: Internet
Author: User

Difference Between Stack and stack
Data Structure stack and stack

First, we need to know the stack in terms of the data structure. Even though we call it this way, the stack is actually two types of data structure: Stack and stack.

Both heap and stack are data structures that sort data items in order.

Stack is like a data bucket or box.

Let's start with the familiar stack.Back-in-first-outData structure, that is, the first to store the data, the first to store the data. This is like taking out what is put under the box (put an earlier object). First, we need to remove the object that is pressed on it (put a later object ).

Heap is like a fallen tree

The heap is different.Sorted tree data structureEach node has a value. Generally, the data structure of a heap refers to a binary heap. Heap is characterized by the smallest (or largest) value of the root node, and the two subtree of the root node is also a heap. Because of this feature of heap, it is often used to implement priority queues, and the access to heap is random. This is like reading books on the bookshelves of the library. Although the books are placed in order, but when we want to retrieve any book, we don't have to take out all the books in front of the stack. Unlike the box, we can retrieve the books we want directly.

Stack and heap in memory allocation

However, the point I want to talk about is not here. The heap and stack I want to talk about is not the heap and stack of the data structure, the reason why we want to talk about the data structure heap and stack is to be different from the heap and stack areas I will talk about later. Please pay attention to them.

Next we will talk about the heap and stack in the C language program memory allocation. It is necessary to mention the memory allocation here. Don't bother me. Generally, the program is stored in Rom or Flash, during running, you need to copy it To the memory for execution. The memory stores different information, as shown in:

If the stack area in the memory is in a relatively high address and the address growth direction is above, the stack address increases downward.

Allocate local variable space in the stack. The heap area increases upwards to allocate the memory space applied by programmers. In addition, static variables and global variables are allocated to static zones. Read-Only zones are allocated with constants and program code spaces, and other partitions.

Let's look at a classic online example:

Main. cpp int a = 0; global initialization zone char * p1; Global uninitialized Zone main () {int B; stack char s [] = "abc"; stack char * p2; stack char * p3 = "123456"; 123456 \ 0 is in the constant zone, and p3 is in the stack. Static int c = 0; Global (static) initialization zone p1 = (char *) malloc (10); heap p2 = (char *) malloc (20); heap}

0. Different application methods and recovery methods

I don't know if you understand it. The first difference between stack and stack is that the application method is different: stack (the English name is stack) is automatically allocated by the system. For example, we define a char; the system automatically opens up space for the stack. Heap (heap) is the space that programmers apply for as needed, such as malloc (10). It opens up ten bytes of space. Because the space on the stack is automatically allocated and automatically recycled, the life cycle of the data on the stack is only in the process of running the function. After running the function, the data is released and cannot be accessed. The data on the stack can be accessed as long as the programmer does not release space, but the disadvantage is that memory leakage will occur once the programmer forgets to release the data. There are other differences. I think my online friends have a good summary and I will repeat it here:

1. system response after application


Stack: As long as the remaining stack space is larger than the requested space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.

Heap: First, you should know that the operating system has a linked list that records the idle memory address. When the system receives a program application, it will traverse the linked list to find the heap with the first space greater than the requested space.

Node, 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 size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list. That is to sayThe heap will do some subsequent work after the application, which will lead to the application efficiency problem.


2. Comparison of Application Efficiency

We can see from and.


Stack: Automatically assigned by the system, which is faster. But programmers cannot control it.

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

3. Application size limit

Stack: In Windows, the stack is a data structure extended to the low address and a continuous memory area. This statement indicates 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, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is small.Heap: The heap is a data structure extended to the high address, and is a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the 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 flexible and large.

4. Storage content in heap and stack

Due to the limited size of the stack, It is physical to use subfunctions, not just logical.

Stack: When calling a function, the first entry to the stack isNext instruction after the function is called in the main function(The next executable statement of a function call statement), followed by the parameters of the function. In most C compilers, the parameters are pushed from right to left, and thenLocal variables in the function. Note that static variables are not included in the stack. When the function call ends, 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, where the program 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.

You can also refer to this question for storage content. This question also involves the survival of local variables.

5. Comparison of access efficiency

Char s1 [] = "aaaaaaaaaaaaa"; char * s2 = "bbbbbbbbbbbbbbbbbbb"; aaaaaaaaaaaaa is assigned a value at runtime and placed on the stack. Bbbbbbbbbbbbb is determined during compilation and placed in the heap. However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap. For example: # include void main () {char a = 1; char c [] = "1234567890"; char * p = "1234567890"; a = c [1]; a = p [1]; return;} corresponding assembly code 10: a = c [1]; 00401067 8A 4D F1 mov cl, byte ptr [ebp-0Fh] 0040106A 88 4D FC mov byte ptr [ebp-4], cl 11: a = p [1]; 0040106D 8B 55 EC mov edx, dword ptr [ebp-14h] 00401070 8A 42 01 mov al, byte ptr [edx + 1] 00401073 88 45 FC mov byte ptr [ebp-4], al

A metaphor for the difference between stack and stack

The difference between stack and stack can be illustrated by the metaphor of a senior: using Stack is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it ), when you are full, you don't have to worry about the preparation work, such as cutting and washing dishes, washing dishes, and so on. His advantage is that it is fast, but his degree of freedom is small. Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom. The metaphor is very vivid and easy to understand. I don't know if you have any gains.

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.