In the process of how the local variables are allocated, and how the function is called in fact, that is to explain the specific use of the stack area. ( The following code map summarizes the network )
First of all, we have to know that the stack is stored in the corresponding function of the stack frame, when the function fun1 is called, then fun1 stack frame into the stack, fun1 return, FUN1 stack frame out of the stack. What is a stack frame, a stack frame is actually a structure that holds a pointer to an executing instruction, a pointer to the stack frame of the key function, a key function passed to the argument of the called function (if any), the local variable of the called function, and so on.
Stack frame structure:
First, we'll show you how to differentiate each stack frame, or how to know which stack frame I'm using now. And the stack is closely related to 2 registers, one is EBP, and one is ESP, the former can be called as the stack base address pointer, the latter can be called stack top pointer. For a stack frame, EBP is also called a stack frame pointer, which always points to a fixed position of the stack frame (see), so you can represent a stack frame based on EBP, and you can go back and forth in the stack frame by adding or minus the offset of EBP. ESP is constantly moving with push and pop. Therefore, the stack frame is manipulated according to ESP.
Again, the topmost part of a stack frame is the argument, and then the return address, which is automatically pressed by the call command in the key function when called, without our concern, previous frame pointer, which is the stack frame pointer of the keynote function, That is, the EBP value of the keynote function. The EBP offsets are positive and are local variables of the modulated function.
1. int function (int a, int b, int c)
2. {
3. Char buffer[14];
4. int sum;
5. sum = a + B + C;
6. return sum;
7.}
8.
9. void Main ()
10. {
one. int i;
i = function (n/a);
13.}
The stack frame where function functions are
Comments:
1.function, buffer is 14 bytes, sum is 4 bytes, as a routine should be requested 18 bytes, but the 11th line, the program requested 20 bytes. This is a tradeoff between time efficiency and space efficiency, because the Intel i386 is a 32-bit processor, and each memory access must be 4-byte aligned, and a high 30-bit address with the same 4 bytes constitutes a machine word. Therefore, if you allocate sum to two different machine words in order to fill the two bytes left by buffer[14], it is obviously unacceptable to have two memory operations per access to sum. These are compiler-related optimization techniques.
2. Let's look at how A, B, C, and sum are assigned in function functions. As mentioned earlier, when accessing arguments and local variables in a function is the stack frame pointer as the base address, plus an offset, and the stack frame pointer under the Intel I386 architecture is EBP, for the sake of clarity, we identified in Figure 7 the offset of all components in the stack frame relative to the stack frame pointer ebp. This 6 in the calculation of 12 to 16 is clear, 8 (? p), three (? p), the (? p) and -20 (? p) are the addresses of the arguments A, B, C, and the local variable sum, and a few simple add instructions and MOV instructions after the execution of sum is a, B, c three. In addition, the return result of the function is passed through EAX in the assembly program generated by the GCC compilation, so the value of sum 17th will be copied to eax in Figure 6.
3. Let's take a look at how the stack frame corresponding to the function functions pops up when it finishes executing. The leave instruction in line 21st of Figure 6 copies the stack frame pointer ebp to the ESP, so the space allocated for the local variable buffer[14] and sum in the stack frame is freed; In addition, the leave directive has a function, is to pop a machine word from the stack and store it in EBP so that EBP reverts to the stack frame pointer of the main function. The RET instruction on line 22nd again pops a machine word from the stack and stores it in the instruction pointer Eip, so the control returns to the ADDL instruction in the 36th line of the main function. The ADDL instruction adds 12 to the stack-top pointer, ESP, and the stack space occupied by the three arguments that were pressed into the stack before the function functions were called is also freed. At this point, the stack frame for function functions is completely destroyed. As mentioned earlier, the return result of the EAX transfer function is passed through the assembly program generated by the GCC compilation, so the 38th line in Figure 6 is stored in the local variable I of the main function.
//
The memory usage of the process is more complex because:
· the memory that the process is requesting is not necessarily actually used
· The memory that is actually used is not necessarily the only process itself (such as dynamic shared library)
effective vm:52120 KB
mapped:352 KB
effective mapped:76.6 KB
Sole use:72 KB
Per file Memory use
LD-2.3.4.SO:VM 94208 B, M 90112 B, S 8192 b
PROG:VM 8192 B, M 8192 B, S 8192 b
LIBC-2.3.4.SO:VM 1180 KB, M 221184 B, S 16384 b
As can be seen, although the virtual address space is 52396KB, the actual mapping (a.k.a. Allocation) space is 352KB, which is consistent with the results of PS. Look again at the value "effective Mapped", which is only 76.6 KB. This value is calculated by:
Valid actual use memory = the process exclusive memory + shared memory A/share A's number of processes + Shared memory B/share B number of processes + ...
Although not very accurate, "effective Mapped" is sufficient to indicate the actual size of the memory occupied by the process.
parameter return values for stack space and distribution of local variables in the programming language process