C + + function parameters pass the Ultimate version of the turn.

Source: Internet
Author: User

function calls:
• Pass data to formal parameters with real parameters;
• Allocate storage space for data parameters and variables declared in the function body;
• Interrupt the current (call) function, and move the process to the entrance of the called function to begin executing the tuned function.
When the parameter table is empty, it means that the function does not accept data from the calling function.

function parameter passing mechanism

The stack store is the primary area where the principal function (procedure) and the called function (procedure) communicate when the call occurs.
There are two basic parameter passing mechanisms: value passing and reference passing.

value Passing (passl-by-value): The formal parameter of the modulated function is treated as a local variable of the modulated function, that is, it opens up in the stack

The memory space is the value of the argument that is put in by the main function and becomes a copy of the argument. The characteristic of value passing is that it is tuned

Any operation of a function on a formal parameter is performed as a local variable without affecting the value of the argument variable of the keynote function.

reference Passing (pass-by-reference): The formal parameters of the modulated function, although also as local variables, open up memory space in the stack

, but this is where the address of the argument variable that is put in by the keynote function is stored. Any operation of the modulated function on the formal parameter is processed into

Indirect addressing, that is, the argument variables in the keynote function are accessed through the addresses stored in the stack. Because of this, the modulated function does the same for the formal parameter.

Affects the argument variables in the keynote function.

Special case: Transfer of constant parameters

In the application parameters often use constant pointers and constant references, such parameters are called constant parameters, the use of parameters will not update the parameters pointed to the object, in the process of parameter passing does not need to execute the copy constructor, thereby improving efficiency.

Constant references are often used in C + +, such as SWAP2 (const int& x; const int& y)
You will not be able to modify the contents of the reference address in the function, specifically, x and Y will not appear to the left of "=".

Instance:

Modify the address of the pointer parameter (distinguish level 2 pointers)

int m=10;
int*n=&m;
void Swap (Int*&a)
{
A=n; A address is replaced by n
}
/******
There are 2 parameters for value passing-pointer variables and non-pointer variables. When a parameter is a pointer variable, it is distinguished from the reference Pass, which refers to the

The value of a PIN variable is passed through the value of an address, in the called function, only modifies the value stored in the memory address, and

The address value has not been modified.

In the C language, value passing is the only available parameter passing mechanism.
******/

Because the pointer variable is affected by the function parameter, it is considered to be a reference pass. This is wrong.

[CPP]View Plaincopy
  1. Example code:
  2. int swap (int *x, int *y)
  3. {
  4. int temp;
  5. temp = *x; *x = *y; *y = temp;
  6. return temp;
  7. }
  8. void Main ()
  9. {
  10. int a = 1, b = 2;
  11. int *P1 = &a;
  12. int *P2 = &b;
  13. Swap (P1, p2)
  14. }
  15. The function swap takes two pointer variables as arguments, and when main () calls swap, the pointer variable p1, the value of P2 (that is, the address of the variable A, b) is placed in the memory unit of the swap on the stack as the form parameter x and Y, in the same way that the value is passed. This can be seen from the following assembly code (note is added by the author):
  16. : void Main ()
  17. 23: {
  18. ......
  19. ......
  20. INT.: int a = 1, b = 2;
  21. 00401088 mov dword ptr [ebp-4],1
  22. 0040108F mov dword ptr [ebp-8],2
  23. INT.: int *P1 = &a;
  24. 00401096 Lea Eax,[ebp-4]
  25. 00401099 mov dword ptr [Ebp-0ch],eax
  26. : int *p2 = &b;
  27. 0040109C Lea Ecx,[ebp-8]
  28. 0040109F mov dword ptr [EBP-10H],ECX
  29. 16:swap (P1, p2);
  30. 004010A2 mov edx,dword ptr [ebp-10h]; parameter P2 value into the stack
  31. 004010A5 push edx
  32. 004010A6 mov eax,dword ptr [ebp-0ch]; parameter P1 value into the stack
  33. 004010A9 push EAX
  34. 004010AA call @ILT +15 (Swap) (00401014); calling swap function
  35. 004010AF add esp,8; Clean up parameters in the stack
  36. 17:}
  37. Read the above code to note that the Intel80x86 series of CPU processing of the stack is generated downward, that is, from the high address cell to the low address cell generation. From the assembly code above, main () before the call to swap, the value of the actual argument in the right-to-left order of the stack, that is, first p2 into the stack, and then p1 into the stack. After the call is over, the main key function () is responsible for cleaning up the parameters in the stack. Swap will use these variable values to enter the stack. The following is the assembly code for the swap function:
  38. : void Swap (int *x, int *y)
  39. 15: {
  40. 00401030 Push EBP
  41. 00401031 mov ebp,esp; ebp points to the top of the stack
  42. ......
  43. ......
  44. : int temp;
  45. 17:temp = *x;
  46. 4: int temp;
  47. 5:temp = *x;
  48. 00401048 mov eax,dword ptr [ebp+8]; manipulate p1 that have been stored on the stack, placing P1 in eax
  49. 0040104B mov ecx,dword ptr [eax]; place *p1 into ECX via register address
  50. 0040104D mov dword ptr [EBP-4],ECX; ecx to place *P1 into the memory unit of the TEMP variable. The following are similar
  51. 6: *x = *y;
  52. 00401050 mov edx,dword ptr [ebp+8]
  53. 00401053 mov eax,dword ptr [ebp+0ch]
  54. 00401056 mov ecx,dword ptr [eax]
  55. 00401058 mov dword ptr [EDX],ECX
  56. 7: *y = temp;
  57. 0040105A mov edx,dword ptr [ebp+0ch]
  58. 0040105D mov eax,dword ptr [ebp-4]
  59. 00401060 mov dword ptr [Edx],eax
  60. 8: return temp;
  61. 00401062 mov eax,dword ptr [ebp-4]
  62. 9:}

The above assembly code basically illustrates the principle of the transfer of value in C, except that the value of the pointer is passed. The SWAP function passed with reference is also discussed later in this article. From these assembly code analysis, here we can get the following points:
1. The stack store of a process is the primary area in which the master function and the modulated function communicate.
2. The parameters in the C language are stacked from right to left.
3. The stack area structure used by the tuned function is:
Local variables (such as temp)
return address
function parameters
Low Address
High Address
4. The stack is cleaned up by the keynote function after the call.
5. The return value of a function is typically placed in a register.
Add a few points: first, the way the parameters into the stack. For internal types, the push instruction is used directly because the compiler knows the size of the memory used for each type of variable, and for a custom type (such as structure), a byte transfer from the source address to the destination (stack area) address is used to stack. The second is why the function return value is generally placed in the register, which is mainly to support interrupts, if placed on the stack may be overwritten because of the interruption. Third, if the return value of the function is large, the byte is transferred from the stack to the address unit that holds the return value (which is provided by the main function before the call to the tuned function) for the purpose of the return. For the second and 3rd, "thinking Inc++" a book in the 10th chapter has a better elaboration. Four is an obvious conclusion that it is meaningless to return the address of the local variable in the function of the call, because the local variable is stored in the stack, and the stack is cleaned up after the invocation, and the addresses become invalid.

function parameter passing mechanism in C + + language


C + + has both a value pass and a reference pass. Consistent with C on value passing, the emphasis here is on reference passing. As mentioned earlier in this article, reference passing is the address of the passed variable to the stack used by the tuned function. Declaring a reference pass in C + + uses the "&" symbol instead of the call. The following code is the SWAP2 function and the main function that are passed using reference

[CPP]View Plaincopy
  1. int& swap2 (int& x, int& y)
  2. {
  3. int temp;
  4. temp = x;
  5. x = y;
  6. y = temp;
  7. return x;
  8. }
  9. void Main ()
  10. {
  11. int a = 1, b = 2;
  12. Swap2 (A, b);
  13. }
  14. At this point the function Swap2 will accept the address of two integer variables and return one of them. The call to SWAP2 from the main function, SWAP2 (A, B), does not see whether reference passing is used or not, and is determined by the definition of the SWAP2 function. The following is the assembly code for the main function:
  15. One: void Main ()
  16. 12: {
  17. ......
  18. ......
  19. INT.: int a = 1, b = 2;
  20. 00401088 mov dword ptr [ebp-4],1; variable A
  21. 0040108F mov dword ptr [ebp-8],2; variable b
  22. 14:swap2 (A, b);
  23. 00401096 Lea Eax,[ebp-8]; The offset address of B is fed into EAX
  24. 00401099 push eax; offset address stack for b
  25. 0040109A Lea Ecx,[ebp-4]; The offset address of a is fed into ECX
  26. 0040109D push ecx; Press the offset address of a to stack
  27. 0040109E call @ILT +20 (SWAP2) (00401019); calling swap function
  28. 004010A3 add esp,8; clean up the parameters in the stack
  29. 15:}
  30. As you can see, the main function offsets B and a in the right-to-left order before calling Swap2.
  31. This is the address at which the variable is passed. At this point the assembly code for the SWAP2 function is:
  32. 2: int& swap2 (int& x, int& y)
  33. 3: {
  34. 00401030 Push EBP
  35. 00401031 mov Ebp,esp
  36. ......
  37. ......
  38. 4: int temp;
  39. 5:temp = x;
  40. 00401048 mov eax,dword ptr [ebp+8]
  41. 0040104B mov ecx,dword ptr [eax]
  42. 0040104D mov dword ptr [EBP-4],ECX
  43. 6:x = y;
  44. 00401050 mov edx,dword ptr [ebp+8]
  45. 00401053 mov eax,dword ptr [ebp+0ch]
  46. 00401056 mov ecx,dword ptr [eax]
  47. 00401058 mov dword ptr [EDX],ECX
  48. 7:y = temp;
  49. 0040105A mov edx,dword ptr [ebp+0ch]
  50. 0040105D mov eax,dword ptr [ebp-4]
  51. 00401060 mov dword ptr [Edx],eax
  52. 8: return x;
  53. 00401062 mov eax,dword ptr [ebp+8]; returns x, since x is the offset address of an external variable, the return is legal
  54. 9:}

As you can see, the SWAP2 is the same as the assembly code of the previous swap function. This is because the previous swap function takes a pointer variable, and the value of the pointer variable is exactly the address. So, for the swap2 here and the previous swap, the function parameters in the stack are all addresses, and the way in which the functions are manipulated is consistent. However, for SWAP2, this address is the keynote function passed in by pressing the offset address of the argument variable-this is a reference pass, and for swap, the address is the keynote function passed in by stacking the value of the argument variable-this is the value passing, Only because this argument variable is a pointer variable so its value is the address.
The key point here is that it is also an address, a variable address in a reference pass, and a value for a pointer variable in a value pass. I think if I could make this clear, I would not confuse the use of pointer variables in C with the passing of the value of a function parameter as a reference.
Although X is a local variable, it is legal to return this address in SWAP2 because its value is the address of the argument variable in the keynote function.

Iv. concluding remarks
In this paper, the parameter passing mechanism of function call in C and C + + is discussed, and some problems of function return value are also provided. The example used in this article is vc++6.0.

Visible value passing is a copy of the variable to be passed, so changing the copy does not affect the calling function, but the called function generally has a useful return value, that is, you use something, in the process of use, may have changed it, but after the time, you remain the same as the others. For example, to give you a good section of the silk scarf, you use a different style, according to the image, while others, but also in accordance with the person's loan you look back to others, and this picture is needed to get something (similar to the return value).

The reference, which is the address of the variable being passed to the called function, is changed in the calling function if it is changed in the called function. For example, if you borrow someone's cloth, if you cut a different style, then people's appearance is the way you cut. General C + + can use value passing and reference passing, the latter more. Because this does not have to be in addition to the space in the stack, and the value of the transfer requires additional space, a certain waste of memory. In general C, only values are used for delivery.

In addition, regarding the storage data aspect, generally is the local variable, the function returns the address, the function parameter puts in the stack, but the function return value generally puts in the register, in order to facilitate the interruption, if has the zero interruption can directly from registers the processing, does not have to carry on the stack operation.

Wrong to get is garbled
Char *strchr (char *str,char character)
{
char* substr;
SUBSTR[0]=STR[0];
return substr;
}

C + + function parameters pass the Ultimate version of the turn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.