C + + value delivery, pointer delivery, reference delivery detailed
Recently wrote several in-depth discussion of arrays and pointers to the article, which refers to the "C language, all non-array form parameter passes are all in value transitive" array and pointer behind--memory angle semantic "trap"---arrays and pointers
And about the value of delivery, pointer passing, reference transmission in these areas there will be misunderstandings, all I feel the need here also explain ~
The following will be illustrated in detail by example.
Value Delivery:
A formal parameter is a copy of an argument, and changing the value of the parameter does not affect the value of the external argument. From the point of view of the invoked function, the value pass is a one-way (argument-> parameter), the value of the parameter can only be passed in,
cannot be outgoing. When the function needs to modify the parameters, and do not want this change to affect the caller, the use of value delivery.
Pointer Pass:
A parameter is a pointer to an argument's address, which is equivalent to an action on the argument itself when pointing to the parameter
Reference delivery:
The formal parameter is equivalent to the "alias" of the argument, and the operation of the formal parameter is actually the operation of the argument, while the formal parameters of the modulated function are also used as local variables in the stack in the reference transfer process.
The memory space is opened, but at this time the address of the argument variable which is put in by the keynote function is stored. Any operation of the called function on the formal parameter is handled as indirection, that is, by
The address in the stack accesses the argument variable in the keynote function. Because of this, any manipulation of the function on the formal parameter affects the argument variables in the keynote function.
The theoretical is not much to say,
This is explained in detail by the following code (the argument, which holds the address in memory, explains the nature of the problem and is easy to understand)
1 #include <iostream>
2 using namespace Std;
3//value transfer
4 void Change1 (int n) {
5 cout<< "value transfer--function operation Address" <<&n<<endl;
6 n++;
7}
9//reference delivery
ten void Change2 (int & N) {
11
n++;
13}
/ /Pointer delivery
void Change3 (int *n) {
16
*n=*n+1;
18
int Main () {
int n=10;
cout<< "The address of the argument" <<&n<<endl;
change1 (n);
cout<< "after Change1 () n=" <<n<<endl;
change2 (n);
cout<< "after Change2 () n=" <<n<<endl;
Change3 (&n);
cout<< "after Change3 () n=" <<n<<endl;
return true;
29}
The results of the operation are as follows (different machines may vary)
As you can see, the address of the argument is 0x22ff44
When the value is passed, the address of the function operation is 0x22ff20 not the argument itself, so manipulating it does not change the value of the argument
Then look at the reference pass, the operation address is the actual parameter address, just equivalent to an alias of the argument, the operation of which is the operation of the argument
Next is the pointer pass, and the operation address is the actual argument address
So what's the difference between a reference pass and a pointer pass?
Rules to refer to:
(1) When a reference is created, it must be initialized (the pointer can be initialized at any time).
(2) cannot have a null reference, the reference must be associated with a legitimate storage unit (the pointer can be null).
(3) Once the reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).
The essence of pointer passing:
The pointer pass parameter is essentially the way the value is passed, and it passes an address value. In the process of value transfer, the formal parameters of the modulated function are treated as local variables of the modulated function,
That is, the memory space is opened in the stack to hold the value of the argument put in by the keynote function, thus becoming a copy of the argument. The characteristic of the value transfer is that the function is modulated to the formal parameter
Any action is taken as a local variable and does not affect the value of the argument variable of the keynote function. (This is to say that the argument pointer itself's address value does not change) if you can't understand it, skip the paragraph.
Pointer passing and reference passing generally apply to:
function to modify the parameters internally and want the changes to affect the caller. A comparison pointer/reference pass can be changed by the parameter "to" the argument (which is actually modified directly on the memory of the argument).
It is not modified to copy the value of an argument to another memory address, like a value pass.
Another use is: when a function actually needs to return more than one value, and can only explicitly return a value, you can pass the other variables that need to be returned as pointers/references
To the function, so that after the function is modified and returned, the caller can get the modified variable, which is equivalent to an implicit return value pass.
Here is what I think about pointers and references to write very good articles, we can refer to the original source address: http://xinklabi.iteye.com/blog/653643
Conceptually speaking. A pointer is essentially a variable that holds the address of a variable, logically independent, that can be changed, including the change in the address it points to and the data that is stored in the address to which it points.
Whereas a reference is an alias, it is logically not independent and its existence is dependent, so the reference must be initialized at the outset, and the object referenced by it cannot be changed throughout its lifecycle (it can only be attached to the same variable from beginning to end).
In C + +, pointers and references are often used for parameter passing of functions, however, pointer passing parameters and reference passing parameters are essentially different:
The pointer pass parameter is essentially the way the value is passed, and it passes an address value. In the process of value transfer, the formal parameters of the modulated function are treated as local variables of the modulated function, that is, the memory space is opened in the stack to hold the values of the arguments put in by the keynote function, thus becoming a copy of the argument. The characteristic of value transfer is that any operation of the function on the formal parameter is taken as a local variable and does not affect the value of the argument variable of the keynote function. (This is to say that the address value of the argument pointer itself does not change)
While in the process of reference transfer, the formal parameters of the modulated function are also used as local variables to open up the memory space in the stack, but at this time the address of the argument variable which is put in by the keynote function is stored. Any operation of the called function on the formal parameter is treated as indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any manipulation of the function on the formal parameter affects the argument variables in the keynote function.
Reference passing and pointer passing are different, although they are a local variable on the stack space of the called function, but any processing of reference parameters will be manipulated to the related variables in the keynote function through an indirection. And for the parameters passed by the pointer, if you change the pointer address in the called function, it will not affect the relevant variables of the key function. If you want to change the related variables in the keynote function by passing pointer arguments, you must use a pointer to the pointer, or a pointer reference.
To further deepen the distinction between pointers and references, let me elaborate on the differences between them from a compilation perspective:
The program adds pointers and references to the symbol table at compile time, where the variable name and the corresponding address of the variable are recorded on the symbol table. The corresponding address value of the pointer variable on the symbol table is the address value of the pointer variable, while the corresponding address value on the symbol table refers to the address value of the referenced object. The symbol table is not changed after it is built, so the pointer can change the object it points to (the value in the pointer variable can be changed), and the referenced object cannot be modified.
Finally, summarize the similarities and differences between pointers and references:
★ The same point:
Are the concept of the address;
The pointer points to a piece of memory whose contents are the address of the memory, and the reference is an alias to a block of memory.
★ Different points:
The pointer is an entity and the reference is only an individual name;
References can only be initialized once at definition, then immutable, pointer variable, reference "one-woman", and pointers may be "inconstant";
The reference does not have a const, the pointer has a const,const pointer is immutable; (specifically, there is no int& const a form, and const int& A is, the former guidelines used in itself that alias can not change, of course, so do not need this form, the latter Guidelines can not be changed with the value indicated
The reference cannot be null, the pointer can be empty;
The "sizeof reference" gets the size of the variable (object) to which it is pointed, and the "sizeof pointer" gets the size of the pointer itself;
The value of the pointer and the reference of the self increment (+ +) operation is different;
References are type-safe, and pointers are not (references are more type-checked than pointers)
I. The concept of references
References introduce a synonym for an object. The presentation of the definition reference is similar to the definition pointer, except that the * is replaced with &.
For example: Point pt1 (10,10);
Point &pt2=pt1; A pt2 is defined as a pt1 reference. With such a definition, pt1 and pt2 represent the same object.
The special emphasis is that the reference does not produce a copy of the object, just a synonym for the object. Therefore, when the following statement is executed:
Pt1.offset (2,2);
Both PT1 and pt2 have values of (12,12).
A reference must be initialized immediately when it is defined, because it must be a synonym for something. You can't just define a reference before
Initialize it. For example, the following statement is illegal:
Point &pt3;
PT3=PT1;
So since the reference is just a synonym for something, what is the use of it?
The two main uses of the reference are discussed below: As a function argument and a left value from a function.
Second, reference parameters
1. Passing Variable parameters
In traditional C, when a function is invoked, the parameter is passed by value, which means that the parameter of the function does not have the ability to return a value.
So in traditional C, if the function's parameter has the ability of returning a value, it is often realized by the pointer. For example, to achieve
The C program for exchanging two integer variable values is as follows:
void Swapint (int *a,int *b)
{
int temp;
Temp=*a;
A=*b;
*b=temp;
}
After using the referral mechanism, the C + + version of the above program is:
void Swapint (int &a,int &b)
{
int temp;
Temp=a;
A=b;
B=temp;
}
The C + + method that calls the function is: Swapint (x,y); C + + automatically passes the X,y address as a parameter to the Swapint function.
2. Passing large objects to functions
When a large object is passed to a function, the use of reference parameters can increase the efficiency of parameter passing because the reference does not produce an object's
Copy, that is, when the parameter is passed, the object does not need to be copied. The following example defines a class with a finite set of integers:
Const MAXCARD=100;
Class Set
{
int Elems[maxcard]; Sets, and Maxcard represents the maximum number of elements in the collection.
int card; The number of elements in the collection.
Public
Set () {card=0;}//constructor
Friend set operator * (set, set); Overloaded operation symbol *, used to compute the intersection of a set with an object as a pass-value parameter
Friend set operator * (Set &, set &) overloaded operation symbol *, used to compute the intersection of the collection with the reference of the object as the parameter of the pass value
...
}
First consider the implementation of set intersection
Set operator * (set Set1,set Set2)
{
Set Res;
for (int i=0;i<set1.card;++i)
for (int j=0;j>set2.card;++j)
if (Set1.elems[i]==set2.elems[j])
{
Res.elems[res.card++]=set1.elems[i];
Break
}
return res;
}
Since overloaded operators cannot operate on pointers alone, we must declare the operands as set type instead of Set *.
Each time you use the * to do the intersection operation, the entire collection is replicated, which is inefficient. We can use references to avoid this situation.
Set operator * (set &set1,set &set2)
{Set res;
for (int i=0;i<set1.card;++i)
for (int j=0;j>set2.card;++j)
if (Set1.elems[i]==set2.elems[j])
{
Res.elems[res.card++]=set1.elems[i];
Break
}
return res;
}
Third, reference return value
If a function returns a reference, then the call to the function can also be assigned a value. Here is a function that has two reference parameters and returns a reference to a double number:
Double &max (double &d1,double &d2)
{
Return d1>d2?d1:d2;
}