[C ++] Detailed description of function parameter transfer in C/C ++

Source: Internet
Author: User

Compare the two transfer methods:

To pass variables by value, you need to copy the Real-Time Parameter memory to initialize the parameters. This will incur some overhead. When discussing simple variables, the overhead of built-in types (such as int and float) is negligible, but for large objects (such as large objects representing the whole 3D World, replication costs are high. The C language also provides a pointer to pass by value, which can also solve the problem of high price in the era of transferring large objects. However, the security problem of "pointer" has been criticized by many programmers. Therefore, C ++ provides a reference-based transfer.

Pass by reference: pass by value cannot modify the value of the real parameter, because it is always a copy of the real parameter; however, pass by reference can modify the value of the real parameter.

 

In some cases, we need to modify the value of the real parameter. In some cases, we do not want to modify the value of the real parameter.

In C language, our solution: if we don't want to modify it, we will directly pass the real parameter. if the object is too large, we will pass the pointer and declare that the pointer is directed to the const object; to modify the value of a real parameter, we can only pass the pointer of the real parameter. However, the pointer cannot be modified with Const.

In C ++, we can still use the method in C, but we can also use the "Reference" method: if you do not want to modify the value of the real parameter, we use the const reference (that is, constant reference) method to modify the value of the referenced object. If you want to modify the value of the real parameter, you can use the common reference parameters (as shown in the preceding procedure ).


The parameter transfer of functions in C or C ++ includes three methods: value transfer, pointer transfer, and reference transfer. These three methods are clearly stated in the programmer interview guide, add my own understanding here.

First look at the source code, the compiling environment is vc6.0, because debugging is convenient. The function is to exchange a and B values.

1 // test. CPP: defines the entry point for the console application. 2 // 3 4 # include "stdafx. H "5 # include <iostream> 6 7 using namespace STD; 8 9 // value transfer cannot be 10 void swap1 (int p, int q) 11 {12 INT temp; 13 temp = P; 14 p = Q; 15 q = temp; 16} 17 18 // pointer transfer, the function body can only change the pointer value 19 void swap2 (int * P, int * q) 20 {21 int temp; 22 temp = * P; 23 * P = * q; 24 * q = temp; 25} 26 27 // pointer transfer, only pointer changes in the function body cannot be performed !!! 28 void swap3 (int * P, int * q) 29 {30 int * temp; 31 temp = P; 32 P = Q; 33 q = temp; 34} 35 36 // The reference transmission can be 37 void swid (Int & P, Int & Q) 38 {39 int temp; 40 temp = P; 41 P = Q; 42 q = temp; 43} 44 45 int main () 46 {47 int A = 1, B = 2; 48 swap1 (A, B ); 49 // swap2 (& A, & B); 50 // swap3 (& A, & B); 51 // swap4 (A, B ); 52 cout <A <"" <B <Endl; 53 return 0; 54}

There are four functions, two of which are pointer passing, but the implementations in the function body are different. The following detailed analysis

1. Value Transfer

The value transfer implemented by the swap1 function is a copy of the actual parameter. If you do not understand this sentence, debug it step by step to check the memory allocation.

When 48 rows are executed, the conditions for A and B are as follows:

Go to the swap1 function body, as shown below:

We can see that the addresses of p and q are different from those of a and B, but the values of A and B are copied, in swap1, the p and q operations only operate on the content of the stack that is temporarily allocated. After the function is executed, the form parameter disappears, and the original A and B are not affected. Therefore, swap1 cannot complete the function of switching A and B values.

2. pointer Transmission

Swap2 and swap3 are pointer passing. The swap2 function exchange the values of p and q pointing to the address. The swap3 function exchanges the addresses pointed to by p and q.

Let's talk about swap2 first and enter the swap2 function body, as shown below:

We can see that the shape parameter pointers p and q point to the addresses a and B, instead of copying the values of the real parameters to the other allocated addresses as the value is passed, when running to the end of the function, for example

We can see that the addresses pointed to by pointers p and q have not changed, but the values in the addresses have changed, that is, the values of A and B have been successfully exchanged, continue debugging and you can see the correct results, as shown in figure

Let's take a look at the following situations when swap3 and swap3 run to the end of the function:

We can see that p and q exchange addresses, but the result after the function is executed is as follows:

The values of A and B are not exchanged. Why?

In swap3, the parameters p and q are stored in the stack. P points to the address of a, Q points to the address of B, and temp pointer is used to complete address exchange between p and q, that is, P points to the address of B, and Q points to the address of a, but the value of A and B does not change, which is different from swap2, in swap2, the value (A) in the address pointed to by P is exchanged with the value (B) in the address pointed to by Q. Therefore, after swap2 is executed, the value of A and B is exchanged.

3. Transfer references

During reference transfer, the operation on the form parameter is equivalent to the operation on the real parameter, that is, the operation will not be a copy of the real parameter, but is the real parameter, enter the swap4 function body as follows:

The memory allocation is clear. Finally, the values of A and B are exchanged.

This is the end. Of course, function parameters can also be pointers, which is also very common, but usually used in places where the memory needs to be dynamically allocated to avoid Memory leakage. When Cuda is used, the cudamalloc parameter is called. This is the pointer to the pointer. Malloc, cplmolloc, and new are the dynamic memory allocated by passing the returned value. Naturally, there will be no memory leakage.

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.