In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about the relationship between stack frame and process in the process of function call. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
In a program, a function is a process, which can be divided into three parts: incoming parameters, procedure code and return. Because a function process needs to use internal variables, temporary variables, etc., it is necessary to allocate a storage segment in the stack space of the process space to store these parameters in the function process, which is the stack frame.
The origin of stack frames:
Provide a store for a function's procedure to store function local variables, parameters, return addresses, and other temporary variables
Structure of stack frames:
Picture source (modern compilation principle)
The period of stack frames:
Enter the function ~ the function returns, and the stack frame in this stage is used as
Different languages are implemented in slightly different ways, but, in general, fun (aforme b)
Local variables:
Including the parameters passed in by the function and the variables defined within the function
Return address:
Instruction pointer P points to call fun, so the return address of fun stack frame storage is Play1; a convention of today's compilers is to store the return address in a fixed register, which is more efficient than reading stack frames (memory).
Temporary variable:
Mainly for calculation, intermediate temporary variables in the process of operation.
Parameter passing:
First, if another function k (a _ maxim b.. n) is called in fun, then the parameters passed are formal parameters. According to the rules of modern compilation, the first k formal parameters are passed through registers, and the last n _ color k formal parameters are passed through the actual parameter part of the stack frame (the tail of the stack frame).
Second: mainly in fun to call the function g (& a), then the argument for the g () function (the parameter is passed through the register) is carried out through the "outgoing argument" block, mainly to ensure that the argument can be accessed by the inner nested function. For example, the g function calls a k (an address) function, which also needs the address of a, so fun must apply for fixed memory storage space (instead of registers) for the argument (& a) when passing parameters so that the k function can obtain parameter values through a fixed memory address (the formal parameter list of fun). At this time, the stack frame of g is the next frame of the fun stack frame (adjacent space address), that is, the stack frame of the caller and the callee is connected.
Protected register:
As a temporary memory storage block of the function process, the stack frame is also responsible for saving and restoring the register value during the function call. That is, assuming that the fun function currently occupies the register Ri to store a temporary variable t, and the function g () is called at this time, the temporary storage of register Ri may be needed in the g () function, then how to ensure that after the g () function call returns, Ri returns to the original value of t in fun. This requires selecting one of the callers or callees to save the original Ri value, that is, caller-save or callee-save.
One last question: what is the embodiment of stack frames in the process space?
Stack space of the process & the growth of stack frames:
The stack frame between the calling function and the called function is adjacent, that is, if the process enters a recursive function f (), the recursive k layer. Then when the k layer is nested, the stack space of the process has produced new k consecutive f () function stack frames, of course, the value of variables stored in each stack frame is determined by the function process.
The above is the relationship between the stack frame and the process in the process of function call. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.