A deep personal understanding of the stack

Publisher:幸福家园Latest update time:2015-01-15 Keywords:Stack Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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 basically 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.
Keywords:Stack Reference address:A deep personal understanding of the stack

Previous article:Solution to the problem of Verilog multiplication result being 0
Next article:HR202 program, basically written in the data sheet

Recommended ReadingLatest update time:2024-11-15 09:23

STC-ISP MCU Download Software and STC-USB Driver Installation Instructions (Tutorial)
Windows XP installation method Open the STC-ISP download software of version V6.79 (or later), and the download software will automatically copy the driver files to the relevant system directory. Insert the USB device, and the system will automatically pop up the following dialog box after finding the device. Se
[Microcontroller]
STC-ISP MCU Download Software and STC-USB Driver Installation Instructions (Tutorial)
PIC16F628 single chip constitutes PVS control system
  This paper takes the use of PIC16F628 single-chip microcomputer to form a PVS control system as an example, starting from the hardware system design and software system design, and gives the printed circuit board diagram and circuit schematic diagram.   Hardware system design   The PVS control system is based on
[Microcontroller]
PIC16F628 single chip constitutes PVS control system
PIC microcontroller interrupt service routine
There is a special definition method for interrupt service routines: void interrupt ISR(void); the function name "ISR" can be changed to any legal combination of letters or numbers, but its input parameter and return parameter types must be "void", that is, there are no input parameters and return parameters, and ther
[Microcontroller]
Give MCU a Fake: Lapis Semiconductor Launches New Car Speech Synthesis Chip
 “Lapis Semiconductor’s new ML2253x series products can not only reduce the software design of the main control MCU, but also build a voice output system. At the same time, it can also use the playback sound abnormality detection function to detect voice problems, which helps to further improve the quality of the voic
[Automotive Electronics]
Give MCU a Fake: Lapis Semiconductor Launches New Car Speech Synthesis Chip
51 MCU Self-study Notes (I) - Use of Keil Software
Understanding MCU Single-chip microcomputer: A microprocessor, memory, and I/O interface circuit are integrated on an integrated circuit chip to form a single-chip microcomputer, namely a single-chip microcomputer. The development of single-chip microcomputers: The development of single-chip microcomputers has gone
[Microcontroller]
51 MCU Self-study Notes (I) - Use of Keil Software
Detailed explanation of 8051 microcontroller interrupt control
IE register interrupt enable and disable flags (1) IE.7 EA: General interrupt enable control bit. EA = 1, all interrupts are enabled, and the enable and disable of each interrupt source can be controlled individually through the corresponding interrupt enable bit; EA = 0, all interrupts are disabled. (2) IE.4 ES:
[Microcontroller]
Traffic light design with time display based on 51 microcontroller
Function: Traffic light design with time display, 30 seconds north-south green light, east-west red light; 3 flashes of yellow light; switch to 30 seconds east-west green light, north-south red light; The specific timing time can be modified according to the program, with source program and simulation diagram. Simul
[Microcontroller]
51 MCU 4*4 matrix keyboard scanning mode
      In actual engineering applications, when there are many keys, matrix keyboards or encoding keyboards are mostly used. For cost considerations, matrix keyboards are generally used.       The following is a partial schematic diagram of the matrix keyboard:               code show as below: #include #defi
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号