Talking about the relation and difference between the reference and the pointer

Source: Internet
Author: User

Why are pointers used by the C + + language? Answer: ① on the one hand, each programming language uses pointers. More than C + + uses pointers.
pointers are used for each programming language. C + + exposes pointers to users (programmers), while languages such as Java and C # hide pointers. "Everything uses pointers. C + + just exposes them rather than hiding them, "It ' s easier to give someone an address to your home than to give a copy of Your home to everyone.② the advantages and necessity of using pointers on the other hand:
  • Pointers can effectively represent the data structure;
  • Can dynamically allocate memory, realize the free management of memory;
  • The use of strings can be more convenient;
  • Easy and efficient use of arrays
  • The pointer is directly related to the storage address of the data, such as: value delivery is not as efficient as address delivery, because the value is passed from the address of the actual parameter, and then the parameter is assigned to the function calculation, and the pointer takes the address of the formal parameter directly to the address of the argument, the use of direct data extraction, efficiency, Especially in the case of frequent assignments (note: Changes in parameters can affect the value of an argument!) )
What is the difference between a reference and a pointer? Nature: References are aliases, pointers are addresses, specific:
① from a phenomenon, the pointer can change the value it points to when it is run, and the reference will not change once it is bound to an object. This sentence can be understood as: The pointer can be re-assigned to point to another different object. But the reference always points to the object that was specified at initialization and cannot be changed later, but the contents of the specified object can be changed.
② from memory allocation, the program allocates memory regions for pointer variables instead of allocating memory regions for references, because the reference declaration must be initialized to point to an already existing object. The reference cannot point to a null value.
Note: The standard does not specify the reference to use memory, there is no provision for the specific reference to how to implement, specifically with the compiler
③ from compilation, the program adds pointers and references to the symbol Table, 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, and ReferencesThe corresponding address value on the symbol table is address value of the Reference object。 The symbol table does not change after it is generated, so the pointer can change the object that is pointed to (the value in the pointer variable can be changed), and the reference object cannot be changed. This is the primary reason for using referential security using pointers that are unsafe. In a way, in a sense, a reference can be considered a pointer that cannot be changed
The fact that ④ does not have a reference to a null value means that using the referenced code is more efficient than using pointers. Because you do not need to test its legitimacy before using a reference. Instead, the pointer should always be tested to prevent it from being empty.
⑤ Theoretically, there is no limit to the progression of pointers, but the reference can only be one level. As follows:
int** P1; Legal. Pointer to pointer
int*& P2; Legal. Reference to Pointer
int&* P3; Illegal. Pointers to references are illegal
int&& P4; Illegal. References to references are illegal
Note that the above reading method is from left to right

Source: >  

The following is an overview of what you can say in plain words:

    • Pointers-For a type t,t* is a pointer to T type, that is, a variable of type t* can hold the address of a T object, and type T can add some qualifiers, such as const, volatile and so on. See, the meaning of the pointer shown:

    • Reference-a reference is an alias for an object that is used primarily for function arguments and return value types, and the symbol x& represents a reference of type X. See, the meaning of the reference shown:

First, the reference cannot be empty, but the pointer can be empty. As mentioned earlier, the reference is an alias of the object, the reference is empty--the object does not exist, how can there be an alias! So when a reference is defined, it must be initialized,do not compile even if not initialized (compile-time error). So if you have a variable that is used to point to another object, but it may be empty, then you should use a pointer, if the variable always points to an object, i.e., your design does not allow variables to be empty, you should use references.
Note: Because the pointer can not point to any object, you must do a null operation before using the pointer, and the reference does not have to.

Second, the reference cannot be changed to point to an object " till death"; but pointers can be changed to point, and point to other objects. Description: Although the reference cannot be changed to point to, it can change the contents of the initialized object.
For example, in the case of the + + operation, the action on the reference directly responds to the object pointed to, not to the point, whereas the manipulation of the pointer causes the pointer to point to the next object instead of changing the contents of the object being referred to.

Again, the size of the reference is the size of the variable you are pointing to, because the reference is just an alias; the pointer is the size of the pointer (address) itself, which is typically 4 bytes under the 32-bit system.
Finally, references are more secure than pointers. Because there is no null reference, and once the reference is initialized to point to an object, it cannot be changed to a reference to another object, so the reference is safe. For pointers, it can point to other objects at any time, and may not be initialized, or null, so it is unsafe. The const pointer cannot change the pointer, but there is still a null pointer, and it is possible to produce a wild pointer (that is, multiple pointers point to a piece of memory, and after a free pointer is dropped, the other pointers become Wild Hands)。

In short, it boils down to the pointer pointing to a piece of memory whose contents are the address of the referred memory, whereas the reference is the alias of a block of memory, and the reference does not change the point. "

In particular, the constWhy would you mention the Const keyword? Because Const has a difference in the qualification of pointers and references: constant pointer vs constant referenceconstant Pointer: A pointer to a constant that is const before the type of the pointer definition statement, indicating that the object being pointed to is a constant.A pointer to a constant restricts only the indirect access operation of the pointer, not the operation of the value itself pointed to by the pointer.
The constant pointer defines "const int* Pointer=&a" to tell the compiler that *pointer is a constant and cannot manipulate *pointer as an lvalue. ★ Constant Reference: A reference to a constant that is const before the type of the reference definition statement, indicating that the object being pointed to is a constant. As with pointers, you cannot reassign a variable to which a reference points.

pointer constant vs Reference constantAdds a const to the pointer name of the pointer definition statement, indicating that the pointer itself is a constant. In must initialize when defining pointer constants! This is a reference to the innate property, without the use of Const. The pointer constant defines "int* const pointer=&b" to tell the compiler that pointer (address) is a constant and cannot operate as an lvalue, but allows modification of the indirect access value, that is, the *pointer (the value of the memory pointed to by the address) can be modified.
Constant pointer constant vs constant reference constant constant Pointer Constants: A pointer constant to a constant that defines a pointer constant pointing to a constant that must be initialized when defined.
defining "Const int* const POINTER=&C" tells the compiler that pointer and *pointer are constants, and they cannot operate as lvalue values.
And there is no so-called constant reference constant, because reference variables are reference constants。 C + + does not differentiate between a const reference to a variable and a reference to a const variable. A program should never reassign a reference to another variable, so the reference is always const. If you apply the keyword const to a reference, the effect is to make its target a const variable. That
No: const double const& a=1; only const double& a=1;
  1. double b=1;
  2. const double& a=b;
  3. b=2;//正确
  4. a=3;//出错error: assignment of read-only reference `a‘

Summary: There is a rule that distinguishes whether Const is a modifier pointer, or the data that the pointer points to-draw a vertical asterisk (*) through the pointer declaration, if const appears online leftthe data pointed to by the pointer is a constant; If the const appears in the RightThe pointer itself is a constant。 The reference itself is a constant, that is, the point cannot be changed.
Pointer passing and reference passing

To better understand pointers and references, the following describes pointer passing and reference passing. How do pointers and references function as arguments to pass values?

    • A pointer pass parameter is essentially the way a value is passed, which is passed an address value. In the process of value passing, the formal parameters of the called 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 key function, thus becoming a copy of the argument. The characteristic of value passing is that any operation of the function on the formal parameter is done as a local variable , without affecting the value of the argument variable of the main key function.
    • In the process of reference passing, the formal parameters of the called function also open up the memory space in the stack as a local variable, but the address of the argument variable that is put in by the key function is stored. Any operation of the modulated function on the formal parameter is handled as an indirection , that is, accessing the real parametric in the central melody function through the address stored in the stack . Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

Reference passing and pointer passing are different, although they are a local variable on the function stack space, but any handling of reference parameters is handled by an indirection to the relevant variables in the key function. For pointers passing parameters, if you change the address of the pointer in the called function, it will not affect the relevant variable of the key function. If you want to change the related variable in the key function by passing the pointer parameter, you have to use a pointer to the pointer, or a pointer reference .

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

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

Finally, summarize the same points and different points of pointers and references:

Same Point :

Are the concept of addresses;

The pointer points to a piece of memory whose contents are the address of the referred memory, and the reference is the alias of 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 the time of definition, immutable, pointers variable, reference "mindedness", pointers "inconstant";

The reference does not have a const, the pointer is const, and theconst pointer is immutable;

Specifically, there is no int& const A in this form, and the const int& A is, the former guideline with itself that alias can not be changed, which is of course, so do not need this form, the latter refers to the value of the reference can not be changed )

The reference cannot be null, the pointer can be empty;

The "sizeof Reference" gets the size of the variable ( object ) pointed to, and the "sizeof pointer" Gets the size of the pointer itself;

Pointer and reference self-increment (+ +) operation has different meanings;

References are type-safe, while pointers are not ( references are more type-checked than pointers)

Reference Documents: Article/details/6921608

From for notes (Wiz)

Talking about the relation and difference between the reference and the pointer

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.