An analysis of the difference between Pointers and references in C++

  • 2020-04-02 02:09:31
  • OfStack

Conceptually. A pointer is essentially a variable that holds the address of a variable. It is logically independent and can be changed, including the address it points to and the data it points to.

A reference is an alias, it is not logically independent, it is dependent, so the reference must be initialized at the beginning, and the object it refers to cannot be changed throughout its life (it can only be attached to the same variable from beginning to end).

In C++, Pointers and references are often used to pass arguments to functions. However, Pointers and references are essentially different:

A pointer passing an argument is essentially a way of passing a value, passing an address value. In the process of value transfer, the formal parameter of the called function is treated as the local variable of the called function, that is, a memory space is created in the stack to store the value of the arguments put in by the main call function, thus becoming a copy of the arguments. The characteristic of value passing is that any operation of the function to the formal parameter is carried out as a local variable, which will not affect the value of the real parameter of the main function.

In the process of reference passing, though the formal parameter of the called function also opens up the memory space in the stack as a local variable, the address of the real parameter put in by the main function is stored. Any operation of the called function on the parameter is treated as indirection, that is, access to the real argument in the main function through the address stored in the stack. Because of this, any operation of the called function on the parameter affects the real parameter in the main function.

Reference passing and pointer passing are different, and while they are both local variables in the called function's stack space, any handling of the referenced arguments is handled by an indirection to the related variables in the calling function. For the parameter passed by the pointer, if the address of the pointer in the called function is changed, it will not affect the relevant variables of the main call function. If you want to change related variables in the main function by passing pointer arguments, you have to use Pointers to Pointers, or pointer references.

In order to further deepen the difference between Pointers and references, I will explain the difference from the perspective of compilation:

At compile time, the program adds Pointers and references to the symbol table, which records the variable name and the address of the variable. The address value of the pointer variable on the symbol table is the address value of the pointer variable, and the address value of the reference on the symbol table is the address value of the reference object. The symbol table is not changed after it is generated, so the pointer can change the object it points to (the value in the pointer variable can be changed), and the reference object cannot be changed.

Finally, summarize the similarities and differences between Pointers and references:

u similarities:

low is the concept of address;

Pointer to a block of memory whose contents are the address of the memory; A reference is an alias name for a block of memory.

u differences:

low a pointer is an entity, and a reference is just an alias;

low references can only be initialized once at definition time and are immutable after that. Pointer variable; Quote "from one to the last", the pointer can be "different";

low reference without const, pointer with const, const pointer immutable;

low references cannot be null, Pointers can be null;

low "sizeof reference" gets the sizeof the variable (object) to which it refers, while "sizeof pointer" gets the sizeof the pointer itself.

low pointer and reference of the self-increment (++) operation meaning is not the same;

low references are type safe, but Pointers are not (references have more type checking than Pointers


Related articles: