BY Value? Or by reference?
PROPUS? Or is it a reference?
WANG Hailong
There is always such an argument on programming parameters. PROPUS? Or is it a reference? (Or a pointer? Or biographical address?) These proposals often appear in the programming technology documentation of C , Java, and C #. This problem also often causes the development of the debate, applying human and labor. In fact, this is not a problem, just because of the concept of human joins, confused people's audiovisual.
From the perspective of the program, the parameter passes, only the pass value, never passing other things. However, the content of the value may be data, or it is possible to be a memory address.
Developers should understand how the program is running in the computer. When the program is running, the space used can be divided into two parts, stacks and stacks. Stack refers to the running stack, local variable, parameter, allocated on the stack. When the program is running, the newly generated object is allocated in the heap, the object assigned to the stack, the data parameters in the stack, or local variables.
A C example will be held below.
Public class object {
INT I;
Public Object (INT I) {
THIS.I = I;
}
Public Int getValue () {
Return I;
}
Public void setValue (int i) {
THIS.I = I;
}
}
Class a {
Void Func1 (int A, Object B) {
Object * c = new object (a);
B = C;
}
Public void main () {
Object * param = new object (1);
Func1 (2, Param);
// What is value of parram now?
// IT IS STILL 1.
}
}
Let's take a look at that when you call the FUNC1 function, run to Object * c = new object (a); the state of the stack and the stack. The results of the code running different compilers may be slightly different. However, the substantially emission order of the parameters and local variables is the same.
... Running Stack
PARAM
Object
(1)
Return Value
Main Addr
Pile of space
Main Addr
A = 2
b
c
Object
(2)
Func1 AddR
At this time, let's see that when the param variable is pressed into the running stack, it is only a simple copy. Copy the contents of the param to b. At this time, B is pointing to Object (1). The parameters here are passed to pass the value of param to B.
Let's take a look, the program is executed to b = c; the stack status is.
... Running Stack
PARAM
Object
(1)
Return Value
Main Addr
Pile of space
Main Addr
A = 2
b
c
Object
(2)
Func1 AddR
We can see that B is now pointing to Object (2). But there is no impact on the value of Param. The value of PARAM is still Object (1).
Therefore, we said that the assignment of the parameters does not affect the data of the outer function, but the operation method of calling the parameters is equal to the data directly operating the outer function. For example, if we do not call B = C in the FUNC1 () function; call B.SetValue (3), the data of Object (1) will become 3, and the data of param can also be changed to 3. The situation in Java and C # is also the same.
There is also a variable definition method that does not meet the above description, which will be described here.
Object * a = new object (1);
Object & * b = a;
The B here is equal to another alias of A, B is a. Assignments to B is equal to assignment to A. This is even as parameters. The assignment of this type of parameter is equal to the assignment of the external function data.
PUBLIC CLASS B {
Void Func1 (Object & * B) {
B = New Object (4);
}
Public void main () {
Object * a = new object (1);
Func1 (a);
// a is change to object (4) Now.
}
}
When FUNC1 (A) is running, the value of A is changed to Object (4). This is because the compiler actually compiles the parameters Object & * B as object ** b_addr, b_addr's value is the address of B, that is, the address of A.
When FUNC1 () is called, it is actually pressed into the B_ADDR as a parameter to the stack, and the value of B_ADDR is the address of A.
When executed B = New Object (4); actually implemented B_addr-> b = new object (4); that is, B_ADDR-> a = new object (4); A value of course is of course changed.
It is also necessary to explain that when developing intermediate parts specification such as COM, CORBA, we need to define the IDL language. The type of parameters is divided into, [in], [out], [in, out], where the RPC remote call parameter packing specification is more complicated, but the principle is the same.