Value Transfer & Reference & pointer transfer call

Source: Internet
Author: User

Related Concepts:

C ++ Parameters

Pass the parameter value (pass the value, or pass the value for short), transfer the parameter address (transfer the address, or transfer the address for short), and transfer the reference (referred to as transfer the reference ), corresponding function value transfer call, address transfer call, and reference transfer call

Parameters and real parameters

When a function is defined, parameters in the parameter table are referred to as formal parameters. When a function is called, parameters in the parameter table are referred to as actual parameters. The data transfer between real parameters and form-and-form parameters is called form-and-reality combination.

Description

Generally, C ++ is a value transfer call, which is unidirectional and can only be used from real parameters to form parameters. The parameter is essentially a copy of the Municipal Real parameter, so the value of the external real parameter is not changed during transmission.

Meanings and functions of references

That is, the alias. The function is mainly used to pass function parameters and return values.

Referenced rule

(1) The reference must be initialized at the same time (the pointer can be initialized at any time ).

(2) there cannot be a null reference, and the reference must be associated with a valid storage unit (the pointer can be null ).

(3) once the reference is initialized, the reference relationship cannot be changed (the pointer can change the object at any time ).

 

 

 

Instance:

# Include <iostream>

Using namespace STD;

 

Void swap (int x, int y );

Int main (void)

{

Int A = 1;

Int B = 2;

Cout <"A =" <A <"," <"B =" <B <Endl;

Swap (A, B );

Cout <"A =" <A <"," <"B =" <B <Endl;

 

System ("pause ");

Return 0;

}

 

I. Value Transfer

Void swap (int x, int y)

{

Int temp = X;

X = y;

Y = temp;

}

Output result:

A = 1, B = 2

A = 1, B = 2

Cause: the swap (int x, int y) function uses the value transfer method. The passed arguments are actually copies of A and B, not itself, therefore, changes to the copy will not be reflected in the and B versions.

 

Ii. Transfer of references

Void swap (Int & X, Int & Y)

{

Int temp = X;

X = y;

Y = temp;

}

Output result:

A = 1, B = 2

A = 2, B = 1

Cause: the swap (int x, int y) function uses the reference transfer method. The passed arguments are actually references of A and B, changes to the reference will be directly reflected on a and B.

 

Iii. pointer Transmission

1. Change the pointer itself

Void swap (int * X, int * Y)

{

Int * temp = X;

X = y;

Y = temp;

}

Call method: swap (& A, & B );

Output result:

A = 1, B = 2

A = 1, B = 2

Cause: the swap (int x, int y) function uses pointer transmission. The passed arguments are actually copies of the pointers of A and B, what is changed is the copy itself rather than its indirect reference, so the pointer pointing to the value that will not be affected, that is, a and B.

 

2. Change the indirect reference of the pointer 

Void swap (int * X, int * Y)

{

Int temp = * X;

* X = * Y;

* Y = temp;

}

Call method: swap (& A, & B );

Output result:

A = 1, B = 2

A = 2, B = 1

Cause: the swap (int x, int y) function uses pointer transmission. Although the passed arguments are also copies of the pointers of A and B, they change the indirect references of the replicas, both the pointer and its copy point to the same value, so this change will be reflected on a and B.

 

Efficiency Comparison:

ProgramCode

// # Include "stdafx. H"

# Include "iostream"

# Include "windows. H"

// # Include "WINBASE. H"

Using namespace STD;

 

Void testvalue (double val1, double val2 );

Void testref (double & val1, double & val2 );

Void testpointer (double * val1, double * val2 );

 

Main ()

{

Double A = 1.5;

Double B = 5.0;

DWORD dwstarttime;

DWORD dwendtime;

 

Dwstarttime = gettickcount ();

Int I;

For (I = 0; I <100000000; I ++)

Testvalue (A, B );

Dwendtime = gettickcount ();

Cout <"\ ntime1 =" <dwendtime-dwstarttime <Endl;

 

Dwstarttime = gettickcount ();

For (I = 0; I <100000000; I ++)

Testref (A, B );

Dwendtime = gettickcount ();

Cout <"\ ntime2 =" <dwendtime-dwstarttime <Endl;

 

Dwstarttime = gettickcount ();

For (I = 0; I <100000000; I ++)

Testpointer (& A, & B );

Dwendtime = gettickcount ();

Cout <"\ ntime3 =" <dwendtime-dwstarttime <Endl;

 

Cin. Get ();

Return 0;

}

 

Void testvalue (double val1, double val2)

{

Double val = val1 + val2;

}

 

Void testref (double & val1, double & val2)

{

Double val = val1 + val2;

}

 

Void testpointer (double * val1, double * val2)

{

Double val = * val1 + * val2;

}

 

Running result

Debug:

First: 1713 1718 1719

Second: 1714 1708 1709

Third: 1713 1718 1719

Under release:

First: 266 266 218

Second: 282 265 219

Third: 266 265 235

 

You can further view the disassembly code and analyze and compare its commands.

Conclusion: Pass reference = pass pointer

 References:

Http://www.cnblogs.com/Jamedy/archive/2007/03/25/687150.html

Http://myturn.blog.hexun.com/15584978_d.html

Http://lovemelovemydogs.blog.163.com/blog/static/9778560200721012092/

 

The above content from: http://www.cnblogs.com/skyseraph/archive/2010/10/25/1860032.html thanks to the author!

I will illustrate the following:

Illustration:

Call to pass the value:

 

 

Address Transfer call:

Reference call:

 

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.