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

Source: Internet
Author: User

I read about memory management yesterday. I have a little bit of knowledge about it, but I am not very thorough. I have not noticed many details when I wrote the code. I only know that this can be implemented, but I don't know why. I don't know the hidden dangers caused by using my own methods. I don't even know that there are other methods that can be implemented, we know that one of the reasons why C ++ is powerful is that there are multiple answers or ideas to a question. I think it may be the reason why it is hard to learn. Because you only know one or a very small number of implementation methods, but you do not know other implementation methods. In other words, you are not very thorough in understanding this thing and have not fully understood it yet. Although sometimes only the tip of the iceberg can solve the problem, a glimpse of the whole picture can make us stand at a higher angle, so that the train of thought or vision in case of problems will not be narrow and limited. It's just one of my thoughts. Go to the topic.

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 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 only changes 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, function body only pointer changes 28 void swap3 (int * P, int * q) 29 {30 int * temp; 31 temp = P; 32 P = Q; 33 q = temp; 34} 35 36 // pass the reference 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.

Related Article

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.