Introduction to the C language stack-difference from the data structure stack

In the computer field, the stack is a concept that cannot be ignored. Basically all C language programs we write are used. But for many beginners, stack is a very vague concept. Stack: a data structure, a place for storage when the program is running, this may be the understanding of many beginners, because I used to think so and confuse the term stack in assembly language. Some programming friends around me and many of the friends I met on the Internet can’t tell you about the stack, so I think it’s necessary to share my views on the stack, and please feel free to say something wrong. Enlighten me, this will be of great help to everyone's study.

Stack and heap
of data structure First of all, we need to know the stack in terms of data structure. Although we call it that, the stack is actually two data structures: heap and stack.

Both heap and stack are data structures in which data items are arranged in order.

Stacks are like buckets or boxes for data

Let's start with the familiar stack, which is a last-in-first-out data structure, that is to say, what is stored later is taken first, and what is stored first is taken first. It's like we want to take out the things under the box (the earlier objects put in), we first need to remove the objects pressed on it (the objects put in later).

Pile like an upside-down tree

The heap is different, the heap is a sorted tree data structure, each node has a value. Generally speaking, the data structure of the heap refers to the binary heap. The characteristic of the heap is that the value of the root node is the smallest (or largest), and the two subtrees of the root node are also a heap. Due to this characteristic of piles, it is often used to implement priority queues, and the storage of piles is arbitrary, which is like picking up books on the shelves of the library. Although the books are placed in order, when we want to pick any one There is no need to take out all the books in front of it like a stack. The book shelf mechanism is different from the box, we can take out the books we want directly.

The 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 are not the heap and stack of the data structure. The reason why I want to say the heap and stack of the data structure is to talk about it later. Please pay attention to the distinction between the stack area and the stack area.

Let’s talk about the heap and stack in the memory allocation of the C language program. It is necessary to mention the memory allocation here. Don’t be too verbose. Generally, the program is stored in Rom or Flash and needs to be copied to the memory for execution. , The memory will store different information separately, as shown in the figure below:

Heap and Stack in Memory Allocation of C Language Program

内存中的栈区处于相对较高的地址以地址的增长方向为上的话,栈地址是向下增长的。

Local variable space is allocated on the stack, and the heap area is used to allocate memory space that the programmer applies for upward growth. In addition, there are static areas where static variables and global variables are allocated; read-only areas are allocated for constants and program code space; and some other partitions.

Let's look at a classic example that is popular on the Internet:

main.cpp 
int a = 0; 全局初始化区 
char *p1; 全局未初始化区 
main() 
{
    
     
int b;char s[] = "abc";char *p2;char *p3 = "123456"; 123456\0在常量区,p3在堆上。 
static int c =0; 全局(静态)初始化区 
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);}

0. The application method and the recycling method are different

I don’t know if you understand it a bit. The first difference between a heap and a stack is the application method: the stack (the English name is stack) is automatically allocated by the system. For example, we define a char a; the system will automatically assign it on the stack. Open up space. The heap (the English name is heap) is the space that programmers apply for by themselves according to their needs, such as malloc (10); open up ten bytes of space. Since the space on the stack is automatically allocated and reclaimed, the life cycle of the data on the stack is only in the running process of the function, and it is released after running and can no longer be accessed. The data on the heap can always be accessed as long as the programmer does not release the space, but the disadvantage is that once forgetting to release it, it will cause a memory leak. There are some other differences. I think friends on the Internet have summarized it well. Here are some examples:

1. System response after application

Stack: As long as the remaining space of the stack is greater than the requested space, the system will provide memory for the program, otherwise an exception will be reported to indicate stack overflow.

Heap: First of all, you should know that the operating system has a linked list that records free memory addresses. When the system receives an application from the program, it will traverse the linked list to find the first heap with a larger space than the requested space.

Node, and then delete the node from the list of free nodes, and allocate the space of the node to the program. In addition, for most systems, this allocation will be recorded at the first address in this memory space In this way, the delete statement in the code can release the memory space correctly. In addition, since the size of the found heap node may not be exactly equal to the size of the application, the system will automatically put the extra part into the free list.

**也就是说堆会在申请后还要做一些后续的工作这就会引出申请效率的问题。**

2. Comparison of application efficiency

According to the first point and the first point.

Stack: automatically allocated by the system, faster. But the programmer cannot control it.

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

3. Application size limit

Stack: In Windows, the stack is a data structure extended to lower addresses, and is a contiguous memory area. This sentence means that the address at the top of the stack and the maximum capacity of the stack are pre-defined by the system. Under WINDOWS, the size of the stack is 2M (some say it is 1M, in short, it is a constant determined at compile time). When the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is smaller.
Heap: Heap is a data structure that extends to high addresses and is a discontinuous memory area. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low addresses to high addresses. The size of the heap is limited by the effective virtual memory in the computer system. It can be seen that the space obtained by the heap is more flexible and larger.

4. Storage content in the heap and stack

Due to the limited size of the stack, the use of sub-functions still has a physical meaning, not just a logical one.

Stack: When a function is called, the first to be pushed onto the stack is the address of the next instruction (the next executable statement of the function call statement) after the function call in the main function, and then the parameters of the function. In most C In the compiler, the parameters are pushed onto the stack from right to left, and then the local variables in the function. Note that static variables are not stacked.
When the function call is over, the local variables are popped out of the stack first, then the parameters, and finally the pointer on the top of the stack points to the initial address, which is the next instruction in the main function, and the program continues from this point.
Heap: Generally, one byte is used to store the size of the heap at the head of the heap. The specific content in the heap is arranged by the programmer.

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

5. Comparison of access efficiency

char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
aaaaaaaaaaa is assigned at runtime; placed on the stack.
And bbbbbbbbbb is determined at compile time; put in the heap.
However, in subsequent accesses, the array on the stack is faster than the string pointed to by the pointer (for example, the heap).
such as:

#include 
void main() 
{
    
     
char a = 1; 
char c[] = "1234567890"; 
char *p ="1234567890"; 
a = c[1]; 
a = p[1]; 
return; 
} 

对应的汇编代码
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

Metaphor about the difference between heap and stack

The difference between stack and stack can be seen by quoting the analogy of an older generation:
using stack is like going to a restaurant to eat, just order (issue an application), pay, and eat (use), and leave when you are full. Regarding preparation work such as cutting vegetables and washing vegetables, and finishing work such as washing dishes and pots, his advantage is that it is fast, but the degree of freedom is small.
Using a pile is like making your own favorite dishes, which is more troublesome, but it is more in line with your own taste and has a lot of freedom. The metaphor is very vivid and easy to understand. I don't know if you have gained a bit.

Guess you like

Origin blog.csdn.net/The_RedMaple/article/details/109146963