C++丨What is the difference between passing by reference and passing by pointer? I got an offer from Dachang

This article mainly introduces the difference between reference passing and pointer passing in C++ (common in interviews). Friends who need it can refer to it.

During the interview, many interviewers asked a question, what is the difference between passing by reference and passing by pointer in C++? Many people should think of the swap function. They only know that it can be implemented by reference or by pointer. As for the difference between the two, I really haven't considered it. After collecting information on the Internet, I sorted it out myself.

 

 Rules cited: 

(1) The reference must be initialized when it is created (the pointer can be initialized at any time). 

(2) There cannot be a NULL reference, and the reference must be associated with a legal storage unit (the pointer can be NULL). 

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

The essence of pointer transfer:

The pointer-passing parameter is essentially a way of value transfer, what it transfers is an address value. In the value transfer process, the formal parameters of the called function are treated as local variables of the called function.

That is, a memory space is opened up in the stack to store the value of the actual parameter put in by the calling function, which becomes a copy of the actual parameter. The characteristic of value transfer is that the modulated function

Any operation is performed as a local variable and will not affect the value of the actual parameter variable of the calling function. (Here is saying that the address value of the actual parameter pointer itself will not change)

Pointer passing and reference passing generally apply to:

Modify the parameters inside the function and hope that the changes will affect the caller. Contrast pointer/reference passing can "pass" changes from formal parameters to actual parameters (in fact, it is directly modified in the memory of the actual parameter,

Unlike value transfer, the actual parameter value is copied to another memory address before modification).

Another usage is: when a function actually needs to return multiple values, but can only return one value explicitly, you can pass the other variables that need to be returned by pointer/reference

To a 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 transfer.

 

So, is there any difference between passing by reference and passing by pointer?

Lite:

Pointer: variable, independent, variable, nullable, stand-in, no type checking;

Reference: alias, dependency, unchanged, non-empty, ontology, type checking;

full version:

1. Concept

  The pointer is essentially a variable, the value of the variable is the address of another variable, the pointer is logically independent, it can be changed, including the value of the pointer variable (the address pointed to) and the corresponding value of the pointer variable The data in the memory (the data stored in the pointed address).

  A reference is essentially an alias, a synonym for another variable, it is not logically independent, its existence is dependent, so the reference must be initialized at the beginning (the variable, this object, this Only physical objects can have aliases), and the referenced objects cannot be changed during their entire life cycle, that is, they can only be attached to the same variable from beginning to end (the alias of which is represented at the time of initialization will always be whose alias, and cannot be changed. ).

2. Pointer parameter passing and reference parameter passing in C++

  Pointer parameter transfer is essentially value transfer, what it transfers is an address value. In the process of value transfer, the formal parameters of the called function are treated as local variables of the called function, and memory space will be opened in the stack to store the actual parameter values ​​passed in by the calling function, thus forming a copy of the actual parameters (stand-in ). The characteristic of value transfer is that any operation of the formal parameter by the called function is performed as a local variable, and will not affect the value of the actual parameter variable of the calling function (the formal parameter pointer changes, and the actual parameter pointer does not change).

  In the process of reference parameter passing, the formal parameters of the called function are also used as local variables to open up memory space in the stack, but at this time the address of the actual parameter variable put in by the calling function is stored. Any operation of the called function on the formal parameters (body) is handled as indirect addressing, that is, the actual parameter variable in the calling function is accessed through the address stored in the stack (the body in the calling function is found according to the alias). Therefore, any operation on the formal parameters of the called function will affect the actual parameter variables in the calling function.

  Passing by reference and passing by pointer are different. Although they are both a local variable on the stack space of the called function, any processing of reference parameters will operate to the relevant variable in the calling function through an indirect addressing method. For the parameters passed by the pointer, if the pointer address in the called function is changed, it will not be applied to the relevant variables of the calling function. If you want to change the relevant variable (address) in the calling function through pointer parameter passing, then you have to use pointer to pointer or pointer reference.

  From the perspective of compilation, the program adds pointers and references to the symbol table when the program is compiled. The symbol table records the variable name and the address corresponding to the variable. The corresponding address value of the pointer variable in the symbol table is the address value of the pointer variable, and the address value corresponding to the reference in the symbol table is the address value of the referenced object (different from the actual parameter name, the address is the same). The symbol table will not be changed after it is generated, so the pointer can change the object it points to (the value in the pointer variable can be changed), but the referenced object cannot be modified.

 

3. Summary

Similarities: the concept of addresses

difference:

1) The pointer is an entity (substitute); the reference is just an alias (another name of the body)

2) The reference can only be initialized once at the time of definition, and it cannot be changed afterwards, that is, "from one to the end"; the pointer can be modified, that is, "change in thought"

3) The reference cannot be empty (only if there is a body, there is an alias); the pointer can be empty;

4) sizeof reference, get the size of the pointed variable; sizeof pointer, get the size of the pointer;

5) Pointer ++ refers to the increment of the address of the pointer; reference ++ refers to the increment of the pointed variable;

6) References are type-safe, and type checking will be performed during the reference process; pointers will not be checked for safety;

Supplement: C language method of passing by reference

#include

#include

int main(int argc, const char * argv[]){

double pi = 3.14;

double intgerPart;

double fractionPart;

fractionPart = modf(pi, &intgerPart);

printf("Pi's Interger Part is %.0f, and Pi's fraction part is %.2f \n", intgerPart, fractionPart);

return 0;

}

Result:

Pi's Interger Part is 3, and Pi's fraction part is 0.14

Program ended with exit code: 0

The above is the difference between reference passing and pointer passing in C++ introduced by the editor, I hope it will be helpful to you.

 

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!

[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112666204