The difference between reference passing and pointer passing in C++

  • 2020-06-03 07:44:01
  • OfStack

During the recent Garena interview, the interviewer asked a question: What is the difference between quote passing and pointer passing in C++?

In my experience, When I think of swap functions, I only know that they can be implemented by reference or by pointer passing. I haven't really considered the difference between the two.

After the pain, masochism, hurriedly make up their knowledge holes.

Through collecting information on the Internet, I also sorted out 1.

Lite version:

Pointer: variable, independent, variable, nullable, double, no type checking;

References: Alias, dependent, invariant, non-null, ontology, type checking;

The full version:

1. The concept

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

Reference in essence is 1 individual name, is synonymous with the other one variable, it is not independent on logic, its existence is dependent, so references must be in 1 start is initialized (to have the first variable, the physical, the physical objects can have an alias), and the reference object throughout its life cycle can't be changed, namely from beginning to end with a variable can only be attached to the (initialization representatives who alias, whose alias is just 1 straight, cannot be changed).

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

Pointer parameter passing is essentially value passing; what it passes is 1 address value. During value transfer, the formal argument of the called function is treated as a local variable of the called function, and memory space is created on the stack to hold the argument values passed in by the calling function, thus forming a copy (surrogate) of the argument. Value transfer is characterized by the fact that any operation of the called function on a formal parameter is performed as a local variable and does not affect the value of the real parameter of the calling function.

During reference parameter passing, the formal parameter of the called function also opens up memory space on the stack as a local variable, but in this case, it holds the address of the real parameter put in by the calling function. Any operation by the calling function on the formal parameter (ontology) is treated as indirection addressing, that is, accessing the real parameter in the calling function through the address stored on the stack (the ontology in the calling function is found by alias). Therefore, any operation of the called function on the parameter will affect the real parameter in the calling function.

Reference passing and pointer passing are different in that, although they are both local variables on the function stack, any handling of reference parameters will operate on the relevant variables in the calling function via an indirection addressing. For arguments passed by Pointers, if you change the address of the pointer in the called function, it will not apply to the variables associated with the calling function. If you want to change the relevant variable (address) in the calling function by passing a pointer argument, use a pointer to a pointer or pointer reference.

From a compilation perspective, at compile time, the program adds Pointers and references to the symbol table, which records the name and address of the variable. The address value of a pointer variable on the symbol table is the address value of the pointer variable, and the address value of a reference on the symbol table is the address value of the reference object (different from the argument name, same address). The symbol table does not change after it is generated, so Pointers can change the objects they point to (values in pointer variables can), but reference objects cannot.

3. Summary

Similarities:

It's all about the concept of address

Difference:

A pointer is an entity (a surrogate); A reference is just 1 individual name (another name for the ontology)

References can only be initialized once at definition time and cannot be changed thereafter, that is, "from one to the end"; The pointer can be modified, that is, "vary";

References cannot be empty (ontology, alias); The pointer can be empty;

sizeof reference, gets the size of the variable it points to; sizeof pointer, which is the size of the pointer;

Pointer ++, refers to the pointer address self-increment; Reference ++ refers to the self-increment of the referred variable;

References are type-safe, and the reference process does type checking; Pointers are not checked for security;

Supplement: C language reference passing method


#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

conclusion


Related articles: