As for the taste of heap, stack, and stack, I have always been like Zhu Bajie eating something, and I have never tasted it carefully. I made up my mind and read a lot of web pages. Here I will organize what I have scraped together.
How do these concepts come from and how to remember them?
Here, I will only summarize my personal understanding of heap/stack/heap in memory management. Other heap/stack in data structure is not within the scope of my summary.
Three concepts are mentioned here: heap, stack, and stack. I equate the concepts of stack and stack. So, next we only need to clarify two concepts: heap and stack.
Let me start with the origin. Since most of my work is related to microcontrollers, my understanding of this aspect is also based on embedded systems.
Each MCU has a certain amount of memory, which is divided into: program storage area; data storage area. For example, every line of code we write will be saved in the program storage area; and some defined global variables, static variables, local variables, etc. are saved in the data storage area.
The program storage area can be said to be a black box for coders: as long as your code does not exceed the program code space, everything will be fine. How the code is executed depends on your programming ideas, that is, if you tell the program to go east, it will go east; if you tell it to stay where it is, it will do the same; the premise is that your code must be well written. Therefore, for people with mature programming experience, for the same algorithm, he may type a few lines of code less, and the probability of algorithm errors is less; people who are not very good at programming, they can type a few more lines and test more. People who study more deeply may also study the efficiency of code execution in depth. I decided to stop here. Continuing with the topic of black boxes, the programmer's code will be burned into the program storage area. As for which statement is stored where, this does not need to be studied. Anyway, once you find that the software does not run as you designed, it is b
asically a problem with the program itself, not the storage area where the program is stored.
The data storage area gives programmers a lot of room to play. Since the program is executed dynamically, the result of the execution and the data generated are not certain.
Let's take an example to explain the relationship between the program and data. The example event is: the operation of the up/down keys of an MP3 player. There are 10 songs in the player. If the current song is the 3rd song, then after pressing the [Up] key, the 2nd song should be played. Therefore, the programmer wrote the program to play the 2nd song. His code makes the [Up] key switch from the 3rd song to the 2nd song. Here, the code is expressed. Moreover, this code is a specific behavior. After pressing the [Up] key from the 3rd song, the 2nd song must be played. This is a specific behavior. If it cannot be implemented, it means that there is a bug in the code and it needs to be corrected. Finally, we can talk about data. What is the 2nd song? In other words, what is the content of the 2nd song? Of course, when the programmer writes the code, he does not know. What he needs to do is to open a data space in advance to store the data related to the song. This data space is flexible and can be used to play whatever you want. After you get tired of listening to these songs, you can change other audio files. To be more specific, a program is like a road that cars can run on; but the programmer does not know what is installed on the car.
Women are talkative. Women can change the subject very quickly.
Heap and stack belong to the category of data storage area, and can also be regarded as means or methods of data management. Based on this, we cannot generalize and say which means is better; they are also generated based on the needs of reality. Hegel said: Existence is reasonable.
This concept is familiar to everyone: military management is very strict and rigid; but for some young technology companies, employees enjoy a high degree of freedom.
If I say, there are 10 books neatly placed in the corner; and the books in the corner are placed messily. If you close your eyes, can you distinguish the two pictures? If not, don't read on.
The characteristic of a stack is that it is strict, orderly, and standardized. If I say, there are books stacked in that corner, you should know how the books are placed.
On the contrary, a pile is free, flexible, and casual. If I say, there are books heaped in that corner, you should know how the books are placed.
2 get closer to the real STACK/HEAP.
Stack: Automatically allocated and recycled by the system.
Heap: Allocated and reclaimed by the programmer.
Based on the understanding of the first part, it is obvious that the data space of the "heap" must also be flexible, because it is unknown what programs thousands of programmers are writing. But one thing we know is that they are running in a certain OS.
Therefore, it is nothing more than giving a name to the data space managed by the system, the stack, and giving a name to the space used by programmers, the heap.
I will talk nonsense next: it doesn’t matter what name we use, what is important is that we have to have a deep understanding of the origin and methods of the management mechanism of these two data storage areas; in this way, even if they are renamed cats and dogs a few centuries later, we can still recognize them.
Example:
void Check_Pro_Code(uint8 style)
{
uint8 i;
switch( style )
{
......
}
}
void main( )
{
uint8 j = 1;
Check_Pro_Code( j );
}
The Check_Pro_Code(...) function is called in the main() function, and j must be pushed onto the stack beforehand. Of course, when calling a function here, several stacking operations are involved: the next execution address of the program; local variables; formal parameters.
I won’t go into detail here.
I am really ashamed that the embedded software I wrote did not involve any heap operation. I am just that kind of person. I did not transplant the operating system into the CM3 kernel. I am really ashamed because I have never dabbled in RTOS. Therefore, my introduction to the heap here is without any practical experience. And I just did it for the sake of heap. Do you think this is academic fraud?
void main( )
{
int j=10;
int *p;
p = malloc( 10 );
Figure 2: How to know how much stack your project uses
It took me 2 hours to compile this article, and I just want to write this to commemorate the beginning of another beautiful life for me.