function parameter Passing in C + + (value passing, pointer passing, reference passing) further understanding

Source: Internet
Author: User

Concept first from the concept of the various functions of the method and the difference: 1, the value of the transfer: The parameter is a copy of the argument, changing the value of the function parameter does not affect the value of the external argument, this is the most commonly used as a method of communication, but also the simplest method of transfer, only need to pass parameters, return value that is , pointer passing: A pointer passing parameter is, in essence, a value pass, which passes an address. "In the process of value passing, the parameters of the called function are treated as local variables of the called function, that is, the memory space is opened in the stack inside the function to hold the value of the argument that is put in by the main function, thus becoming a copy of the argument (remember this, the parameter in the function is a copy of the arguments)".   Because the pointer passes the address of the external argument, the natural external argument value changes when the parameter value of the function is changed. 3. Reference passing: Although the parameters of the modulated function also open up memory space in the stack as local variables, the address of the argument variable placed by the main function is stored in the stack. Any operation of the modulated function on the formal parameter is handled as an indirection, that is, by accessing the real parametric in the central melody function through the address stored in the stack (the arguments and parameters are combined by reference, the white point is: a person, there are two names of that, the latter would like to elaborate).   Therefore, any changes to the parameters directly affect the arguments. The example begins with a simple example:
    1. Value passing: Example skipped.
    2. Pointer passing:
[CPP]View Plaincopy
  1. void swap (int *a,int *b)
  2. {
  3. int temp;
  4. Temp=*a;
  5. *a=*b;
  6. *b=temp;
  7. cout<<"a=" <<a<<"," <<"b=" <<b<<endl;
  8. cout<<"*a=" <<*a<<"," <<"*b=" <<*b<<endl;
  9. cout<<"&a=" <<&a<<"," <<"&b=" <<&b<<endl;
  10. }
(in the case of a new university, the exchange value) is called: [CPP]View Plaincopy
    1. int main () {
    2. int x=1;
    3. int y=2;
    4. cout<<"x=" <<x<<"," <<"y=" <<y<<endl;
    5. cout<<"&x=" <<&x<<"," <<"&y=" <<&y<<endl;
    6. Swap (&x,&y);
    7. }
Be sure to remember this call method [CPP]View Plaincopy
    1. Swap (&x,&y);
As described in the concept of pointer passing, the address is passed to the formal parameter. The form: int *a = &x;//is used for pointer passing, a has its own independent memory address, the stored content is the address of X, *a is the value of x stored. Output Result:

The initial state (address state) of each variable passing in the value: A (b) is a pointer to an external argument address, and *a (*B) is the content of the pointer, which, if changed *a (*B), will inevitably result in changes to the external arguments.              After Exchange: *a=2,*b=1;          The result is that a or B pointer points to the address of X or Y, so the external arguments change because the *a,*b is worth swapping.        Consider whether the following actions are worth the change? Simple test Code: [CPP]View Plaincopy
  1. int change (char* name) {
  2. cout<<"*******change--before******" <<endl;
  3. cout<<"Name=" <<name<<endl;
  4. cout<<"*name=" <<&name<<endl;
  5. Name="Alter";
  6. cout<<"*******change--after********" <<endl;
  7. cout<<"Name=" <<name<<endl;
  8. cout<<"*name=" <<&name<<endl;
  9. return 1;
  10. }
  11. int main ()
  12. {
  13. char *str = "This is a test";
  14. cout<<"******main--before*****" <<endl;
  15. cout<<"str=" <<str<<endl;
  16. cout<<"*str=" <<&str<<endl;
  17. Change (str);
  18. cout<<"*****main--after*****" <<endl;
  19. cout<<"str=" <<str<<endl;
  20. cout<<"*str=" <<&str<<endl;
  21. return 1;
  22. }
Execution results: (when printing output, a bit of error, *STR should be &str) from the results found, did not achieve the change is worth the effect, why? This test code is the same as the question at the beginning of this article, then further analysis: the initial state (address state) of each variable passing in the value: performing an assignment operation [CPP]View Plaincopy
    1. Name="Alter";
The system first needs to allocate a memory space (address) to the string "alter" before the pointer points to its address. So the *str didn't change, so the final print is still "This is a test", which explains my confusion at first! Another pointer that succeeds in passing parameters to the calling method----pointer to pointers: [CPP]View Plaincopy
    1. void my_malloc ( Void** p, int size)   
    2. {   
    3.     *p = malloc (sizeof (int) *size);   
    4. }  
    5. int main ()   
    6. {  
    7.      INT&NBSP;*A;&NBSP;&NBSP;
    8.     my_malloc (&a &NBSP;,&NBSP;10);   
    9.     return 1;   
    10. }  
Execution Result: (some parameters are not used, just to print it out to see) when we do not have to allocate space for *p: the figure after the execution of malloc (size) is as follows: After assigning to *p: Because P points to &a is the address of a, *p points to the value in the address of a. Now it's time to point the allocated memory to *p, so the value of a is the newly allocated memory!     (This is difficult to circle) then, we allocated the memory for pointer a successfully. 3. Reference Delivery: [CPP]View Plaincopy
  1. void Swapref (int &a,int &b)
  2. {
  3. cout << "******************before swapref:******************" <<endl;
  4. cout<<"a=" <<a<<"," <<"b=" <<b<<endl;
  5. cout<<"&a=" <<&a<<"," <<"&b=" <<&b<<endl;
  6. int temp;
  7. Temp=a;
  8. A=b;
  9. B=temp;
  10. cout << "******************after swapref:******************" <<endl;
  11. cout<<"a=" <<a<<"," <<"b=" <<b<<endl;
  12. cout<<"&a=" <<&a<<"," <<"&b=" <<&b<<endl;
  13. }
  14. int main () {
  15. int x=1;
  16. int y=2;
  17. cout<<"******main--before*****" <<endl;
  18. cout<<"x=" <<x<<"," <<"y=" <<y<<endl;
  19. cout<<"&x=" <<&x<<"," <<"&y=" <<&y<<endl;
  20. //swap (&x,&y);
  21. Swapref (x, y);
  22. cout<<"*****main--after*****" <<endl;
  23. cout<<"x=" <<x<<"," <<"y=" <<y<<endl;
  24. cout<<"&x=" <<&x<<"," <<"&y=" <<&y<<endl;
  25. }
Be sure to remember this call method
[CPP]View Plaincopy
    1. Swapref (x, y);
Shaped like: int &a=x; // for reference passing, it can be understood that A is x,x is a, but the name is not the sameExecution results: This is not the specific analysis, remember that the reference pass argument and formal parameters are the same, but the name is different.       Summary: The focus of this article is still in the parameters of the pointer, the pointer is really a headache, today encountered, will be wrong, to understand later, and so on, and forget, and encountered errors, again, so repeatedly, hope to continue to improve, the understanding of the pointer constantly deepened! Wrote an afternoon, side pondering, side reference, side experiment, hope to you useful! Please forgive me for the mistake! Transferred from: http://blog.csdn.net/richerg85/article/details/14450183

function parameter Passing in C + + (value passing, pointer passing, reference passing) further understanding

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.