Patient: What happens when a doctor, a local variable beyond the role domain? I have a headache for this long. Chinese medicine: Oh, they can't be accessed, dying. Your disease will not be so simple, what is the problem, described in detail. Patient: I want to know that the local variable refers to the pointer, after the role domain, the behavior of the pointer. For example, this program #include "stdio.h" int main (int Argc, char * argv []) {INT i = 10; int * pitotest = & i; printf ("% d / n", * pitotest); {INT Igone = 20; pitotest = & omen; // <--- 咔} Printf ("% d / n", * pitotest); // <- ah Return 0;} Figure 1. Run results 20 ", it is * Pitotest's value before, IGONE is actually done, always live in our hearts? Chinese medicine: Well, what do you want to say like this? " With terms, where is its Storage? Patient: Oh, don't know. Chinese medicine: This is right, your nature is the Storage problem of local variables, so you will take a headache. Now this question first puts it, you don't know the mechanism of function to pass parameters? Patient: Chinese medicine: We have to start here. Each function call, there must be a memory to save its parameters (if you do not assume that all functions have some parameters), and the compilation moment does not know how to call a function (recursive), how many parameters should you stay for him? Space, right? patient:. . . Yes, even if there is a main function, it does not hinder him to call himself N times M layer. Places where parameters must be a very "dynamic" place. Chinese medicine: This is very dynamic, the term stack (referred to as "stack"). The operation mechanism and the stack in the data structure are all advanced after all operations occur in the top of the stack, but the stack here is implemented by the CPU and OS. When the function is called, 1 put its parameters push into the stack, 2 put the return address Push into the stack, 3 jump to the function entry address. Void Func1 (int A, int b); ... func1 (10, 20); ... ┌ ------------------ ┐ │ return address │ -------------------- ┤ │ 左 - 10 │ ├ ------------------------------------------------------------------------------------------------------------------------------ ┤ │ The rightmost parameters - 20 │ ├ -------------------- ┤ │ previous stack │ Figure 2. Enter the stack situation of the FUNC1 moment (assuming the stack up Growth parameters to the left in the left, and finally the address is now, what should you happen when the function returns? Patient: I guess it is the reverse sequence called. (1) Get the return address jump back, (2) Stack restore to "previous stack" Chinese medicine: Very good, please remember (1) is Callee, and (2) is Caller's responsibility. After reading the disease, you think about the mechanism of the PRINTF's unique parameter function to understand the need. But now we have to organize the process of function call.
1Caller put its parameter PUSH into the stack 2Caller put the return address Push into the stack 3 jump to the function entry address. 4Callee's function body is executed 5Callee to get the return address jump back to the 6Caller to restore the stack to "previous stack" Figure 3. Process patient calling: Well, these I understand. This is the relationship between this and my disease root "STORAGE problem"? Chinese medicine: Is it really understood? This also implies a premise, Callee must get the return address. Patient: This is also pushing, isn't it in the top of the stack? Chinese medicine: Hey, you also implied a premise, in the function body, in addition to the "stack balanced operation" such as call function, the stack is not growing, otherwise the return address will not be able to make you at the top. Patient: Isn't it? . . what! Is the local variable into the stack in the body? The roles of local variables and the parameters of the function are very close, and his Storage should also be a stack! Chinese medicine: I can realize this, strong. Let's refine the function of Figure 3. 4.1 Put the local variable into the stack (how many local variable compilers in the function is clear) 4.2 Function code 4.3 Restoring the stack to "the stack when entering the function" (the aunt, the reverse operation of .1) Figure 4. Functional refinement ┌ -------------------- ┐ │ │ │ Each partial variable │ │ │ --------- --------- ┤ │ Return Address │ -------------------- ┤ │ 左 参 左 参 - 10 │ ├ ----- --------------- ┤ │ The rightmost parameters - 20 │ ├ -------------------- ┤ │ Stack │ Figure 5. Local variables are given in the stack (how many local variable compilers in the function are clear), which is indeed possible to return to the return address when returning. Incommpt, do you talk about how to locate a specific parameter in the function code? Patient: You can always be on the top of the stack! The compiler sets the offset of the top of the stroke of this function. In the function code, each variable is determined based on the top of the stack and that offset at the time. and many more! My disease, I think. . . ┌ -------------------- ┐│IGONE = 20 │ │PITOTOTEST │ │i = 10 │ -------------- ------ ┤ │ Return Address │ -------------------- ┤ │ 左 左 参 - 10 │ ---------- ------------ ┤ │ Theft of the rightmost parameters - 20 │ ├ -------------------- ┤ │ 之 之 栈 │ Figure 6. When running to "咔咔", the profile of the stack. Due to the C compiler, only the sectors and structures generate a desiratory function, do not clean up in simple type variables (if an int is equipped with a destructive function, the C can also be efficient), so Igone demives After that, his space is still there, no one goes with that stack position, then "ah", IGONE is not surprising. Chinese medicine: reasoning is correct! Patient: It seems that this disease seems to be slight, in fact, the root is very deep! Chinese medicine: Is it now? Patient: Most good, if you take me to see the assembly code generated by the compiler. Chinese medicine: Hey, veto.