C language stack entry-stack and stack differences

Source: Internet
Author: User

In the computer field, stack is a concept that cannot be ignored. The C language programs we write are basically used. But for many beginners, stack is a very vague concept. STACK: a data structure, a place to store when the program is running, which may be a recognition of many beginners, because I used to think so and confuse the word stack in assembly language. Many of my programming friends and friends who have posted posts online cannot tell the stack, so I think it is necessary to share my views on the stack, if something is wrong, please give me some advice. This will be of great help to everyone.

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.

Let's start with the familiar stack. It is a kind of data structure with the nature of "back-in-first-out". That is to say, it stores the data first and then stores the data first. 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 ). The heap is different. The heap is a Sort tree data structure with a value for each node. 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.

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 in the memory is in a relatively high address and the address growth direction is above, the stack address increases downward, and the stack allocates local variable space, the heap area increases upwards to allocate 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; uninitialized globally
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
}
  
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 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: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application, it will traverse the linked list, find the heap node with the first space greater than the requested space, 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 say, the heap will do some subsequent work after the application, which will lead to the application efficiency problem.

2. Comparison of Application Efficiency
The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
The heap 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, a stack is a data structure extended to a 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 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
STACK: When a function is called, the first entry to the stack is the address of the next instruction (the next executable statement of the function call statement) after the function is called in the main function, then there are various parameters of the function. In most C compilers, the parameters are from right to left and then the local 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.

5. Comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaa ";
Char * S2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime;
Bbbbbbbbbbbbb is determined during compilation;
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

The difference between stack and stack can be seen from the metaphor of a senior:
  
Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave, without having to worry about the preparation work, such as cutting and washing dishes, as well as the cleaning and tail work such as washing dishes and flushing pots, his advantage 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.