Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the stack guide of C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the stack guidelines of C++, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

We often talk about when data is stored in the Stack and when the data is stored in the Heap. We know that local variables are stored in the stack; when debug, look at the stack to know the order in which functions are called; passing parameters when a function is called is actually pushing parameters into the stack, which sounds like a hodgepodge. So how exactly does the Stack work? This article will explain in more detail the working mechanism of the Cramp Candle + stack. Please pay attention to the following points when reading:

1) the compilation environment discussed in this article is VisualC/C++, because the stack mechanism of the high-level language is roughly the same, so it also makes sense for other compilation environments or high-level languages such as C #.

2) the stack discussed in this article refers to the default stack assigned by the program to each thread to support the running of the program, rather than the stack defined by the programmer to implement the algorithm.

3) the platform discussed in this paper is intelx86.

4) the main part of this article will try to avoid the knowledge of assembly. The decompiled code and comments of the previous chapter are given in the optional chapter at the end of this article.

5) structured exception handling is also implemented through the stack (when you use try... The catch statement uses C++ 's extension of structured exception handling for windows), but the topic on structured exception handling is too complex to cover in this article.

Start with some basic knowledge and concepts

1) the stack of the program is directly supported by the processor. In intelx86's system, the stack expands from a high address to a low address in memory (unlike a custom stack extending from a low address to a high address), so the address at the top of the stack is decreasing, and the later the data enters the stack, the lower the address.

2) in a 32-bit system, the size of each data unit in the stack is 4 bytes. Data less than or equal to 4 bytes, such as bytes, words, double words, and Boolean types, takes up 4 bytes in the stack; data greater than 4 bytes takes up an integral multiple of 4 bytes in the stack.

3) the two registers related to stack operation are the EBP register and the ESP register. In this article, you only need to understand EBP and ESP as two pointers. The ESP register always points to the top of the stack. When you execute the PUSH command to push data into the stack, ESP minus 4, and then copy the data to the address that ESP points to; when you execute the POP command, first copy the data pointed to by ESP to the memory address / register, and then add 4 to ESP. EBP register is used to access the data in the stack, it points to a location in the middle of the stack (the specific location will be explained later), the parameter address of the function is higher than the value of EBP, while the address of the local variable of the function is lower than the value of EBP, so the parameter or local variable is always accessed through EBP plus or minus a certain offset address, for example, the first parameter to access the function is EBP+8.

4) what data is stored in the stack? It includes the parameters of the function, the local variables of the function, the value of the register (to recover the register), the return address of the function, and data for structured exception handling (when there is a try in the function. Only when the catch statement, which is not discussed in this article. The data is organized in a certain order, which we call a StackFrame. A stack frame corresponds to a function call. At the beginning of the function, the corresponding stack frame is fully established (all local variables are allocated space when the function frame is established, rather than constantly created and destroyed with the execution of the function); when the function exits, the entire function frame will be destroyed.

5) in this paper, we call the caller of the function as caller (caller) and the called function as callee (callee). This concept is introduced because of the creation and cleaning of a function frame, some work is done by Caller and some work is done by Callee.

After reading the above, do you have any further understanding of what is C++ 's stack guide? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report